2014-08-30 68 views
1

對於我的主頁,我想顯示特定類別(例如類別「食物」)的帖子,而不是所有帖子的默認顯示。爲主頁的特定類別循環

這是如何實現的?是真的,我應該使用wp_query()並避免query_posts()? 此外,我是否需要在index.php中使用wp_reset_postdata();重置此循環?

我已經閱讀了代碼和google搜索,但仍然沒有信心改變循環。

我想下面的代碼的工作,但我得到的死亡的白色屏幕在WordPress主題2012年和2013年我取代了代碼index.php

<?php 
$query = new WP_Query('cat=1'); 
if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); 
     the_title(); 
    endwhile; 
endif; 
?> 

編輯:的

白色屏幕死亡是由於我的index.php文件中其他地方丟失了?>。 Noob錯誤。但是現在我遇到了分頁問題。第2頁顯示與第1頁相同的帖子。什麼可能是錯誤的?

我做分頁內functions.php

if (! function_exists('my_pagination')) : 
    function my_pagination() { 
     global $wp_query; 
     $big = 999999999; // need an unlikely integer 
     echo paginate_links(array(
      'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 
      'format' => '?paged=%#%', 
      'current' => max(1, get_query_var('paged')), 
      'prev_next' => True, 
      'total' => $wp_query->max_num_pages 
     )); 
} 

代碼。

編輯:

我在下面使用了Pieter Goosen的解決方案。問題修復。

+0

該代碼塊沒有被破壞,所以可能在頁面上的其他地方出現了錯誤。你有沒有檢查你的錯誤日誌? – rmmoul 2014-08-30 18:45:05

回答

1

您不應在主頁或任何存檔頁面上更改自定義查詢的主要查詢。自定義查詢適用於輔助內容,而不是顯示您的主要內容。我已經在WPSE上就此事做了一個非常詳細的帖子,你可以查看here。它也涵蓋了一點,爲什麼你應該從來沒有使用query_posts

爲了解決你的問題,從您的index.php刪除您的自定義查詢,並且只需添加默認迴環像

if(have_posts()){ 
    while(have_posts()) { 
    the_post(); 

    //YOUR LOOP ELEMENTS 

    } 
} 

現在,您將再次看到主頁上的所有文章。要只在主頁上顯示選定的類別,請在執行主查詢前使用pre_get_post來更改主查詢。添加到您的functions.php

function show_only_category($query) { 
    if ($query->is_home() && $query->is_main_query()) { 
     $query->set('cat', '1'); 
    } 
} 
add_action('pre_get_posts', 'show_only_category'); 

這將解決你的問題分頁以及

編輯

那麼用戶着想,我已經轉載我的答案從WPSE到this question由相同的OP

+0

非常感謝Pieter!這工作完美,分頁工作正常!我對術語有點困惑。 「自定義查詢」總是如下所示:----- $ query = new WP_Query('cat = 1'); -----?我很困惑,因爲我沒有看到[Codex for custom query]中的確切代碼(http://codex.wordpress.org/Custom_Queries)-----另外,query_posts()也被稱爲自定義查詢? – leko 2014-08-31 15:54:21

+0

我對此術語感到困惑也是因爲[關於重置循環的文章](http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/)----- wp_reset_postdata() - >在使用WP_Query創建自定義或多個循環後最好使用----- wp_reset_query() - >在query_posts循環重置自定義查詢後最好使用-----他將query_posts稱爲自定義查詢 – leko 2014-08-31 15:57:41

+1

以下內容被用作自定義查詢,即WP_Query','get_posts'和'query_posts'。關於循環的codex頁面寫得很糟糕。不應該使用'query_posts'。使用'WP_Query'和'get_posts'創建的自定義查詢應該用'wp_reset_postdata()'重置。 'query_posts'應該用'wp_reset_query'重設 – 2014-08-31 17:00:30

1
<?php 
    get_header(); ?> 

    <div id="container"> 
     <div id="content" role="main"> 

     <?php 
      $categories = get_categories(); //get all the categories 

      foreach ($categories as $category) { 
       $category_id= $category->term_id; ?> 

       <div><?php echo $category->name ?></div> 

       <?php 
        query_posts("category=$category_id&posts_per_page=100"); 

        if (have_posts()) { 
         while (have_posts()) { 
          the_post(); ?> 

       <a href="<?php the_permalink();?>"><?php the_title(); ?></a> 

          <?php } 
         } 
        } 
      } ?> 

     </div> 
    </div>