2013-04-22 253 views
0

我一直在努力從Wordpress中提取父類別的帖子。當我試圖顯示唯一的父類別發佈wordpress查詢顯示子類別帖子以及。WordPress - 只顯示父類別的帖子不是子類別的帖子

如何忽略子類別的帖子。請幫助!!

<? 
// Get the post ID 
     $id = get_the_ID();   


     //get all the categories (this function will return all categories for a post in an array) 
     $category= get_the_category($id); 

     if ($category->category_parent == 0) { 

     //extract the first category from the array 
     $catID = $category[0]->cat_ID; 

     //Assign the category ID into the query 
     $verticalNavigationSwitcher = "cat=$catID&orderby=ID&order=ASC"; 
    }    


     $result = new WP_Query($verticalNavigationSwitcher); 


        //$featuredPosts->query('showposts=5&cat=3'); 
        while ($result->have_posts()) : $result->the_post(); 
     ?> 


    <li><a href='<?php the_permalink() ?>'><span><?php the_title(); ?></span></a></li> 


    <?php 
      endwhile; 
      wp_reset_postdata(); 
?> 
+0

你真的在你的問題不應該張貼outcommented代碼。這樣做,並修復你的代碼縮進(我很高興,但不知道什麼時候很難說)。 – timss 2013-04-22 05:45:50

+0

代碼有什麼問題?它會顯示父類別和子類別的所有帖子..它對每個安裝的單詞按鍵都有效。 – Anam 2013-04-22 05:49:23

+0

Outcommented代碼和代碼縮進,而不是你的代碼實際上做什麼。 – timss 2013-04-22 05:50:43

回答

0

嘗試使用此查詢:

$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC"; 
+0

謝謝。簡單的改變讓我的生活變得更輕鬆......再次感謝 – Anam 2013-04-22 11:30:03

相關問題