2014-12-23 20 views
0

我目前有一個安裝程序,我按順序調用了以下文件。只在一個部分運行wordpress查詢

在我的header.php

,我打電話<?php get_theme_part('front', 'current-front'); ?>

該文件的內容是

<?php query_posts("showposts=1&cat=6"); ?> 

<?php 
/** 
* The template for displaying list of recent posts on the front page. 
* 
* @package WordPress 
* @subpackage xxx 
* @since xxx 1.0 
*/ 
if (!have_posts()) 
    return; 
?> 

<div class="front-current-print"> 
     <div class="current-print"> 
      <?php while (have_posts()) : the_post(); ?> 
       <?php get_theme_part('current-print'); ?> 
      <?php endwhile; ?> 
     </div> 
</div> 

該文件被調用電流print.php,其中有這樣的代碼:

<?php 
?><article <?php post_class('compact bg-secondary'); ?>> 
    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail('current-print'); ?></a> 
    <h5 class="title"> 
     <a class="inverse" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
    </h5> 
    <div class="entry-summary"><?php the_excerpt(); ?></div> 
    <?php the_post_episode(); ?> 
</article> 

此代碼允許我爲特定類別的特定帖子製作縮略圖。

這工作正常的頭版,但似乎所有內頁和帖子,它只會顯示類別和帖子。我試圖拿出<?php query_posts("showposts=1&cat=6"); ?>和帖子通過罰款。有沒有什麼辦法可以讓腳本只在標題中運行,而不會影響網站的其他部分?

回答

1

您需要使用wp_reset_query();

wp_reset_query()恢復$ wp_query和全球POST數據到原來的主查詢。如果您必須使用該函數,則應在query_posts()之後調用此函數。

參見:http://codex.wordpress.org/Function_Reference/wp_reset_query

你的情況,這將是:

<?php query_posts("showposts=1&cat=6"); ?> 

<?php 
/** 
* The template for displaying list of recent posts on the front page. 
* 
* @package WordPress 
* @subpackage xxx 
* @since xxx 1.0 
*/ 
if (!have_posts()) 
    return; 
?> 

<div class="front-current-print"> 
     <div class="current-print"> 
      <?php while (have_posts()) : the_post(); ?> 
       <?php get_theme_part('current-print'); ?> 
      <?php endwhile; ?> 
     </div> 
</div> 

<?php wp_reset_query(); ?> 
+1

是啊。或者爲你的頭部查詢使用一個全新的WP_Query對象 - 在頁面上有很多方法可以包含[多個循環](http://codex.wordpress.org/The_Loop#Multiple_Loops)。 –