2013-04-22 171 views
1

假設我有以下幾類。僅顯示來自一個子類別的文章 - Wordpress

Movies 

----- Action 
----- Science fiction 
----- Drama 

Music 

----- POP 
----- Rock 

這裏,電影和音樂是父類別,其餘的是子類別。

如果我只想顯示音樂類別中的POP音樂(POP的帖子),我該怎麼做。

請幫忙!

我不知道如何顯示子類別的帖子。只有我有一個示例代碼,可以顯示父和子類別的帖子。

<? 
// 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(); 
?> 
+1

舉一個例子你如何顯示一般結果,對於不熟悉WordPress – 2013-04-22 13:11:57

+0

的人顯示你的代碼。 – Rikesh 2013-04-22 13:16:01

+0

嗨Royal和Rikesh,我添加了一個示例代碼。目前我不知道如何顯示子類別的帖子。此代碼將顯示來自類別的所有帖子及其子類別。 – Anam 2013-04-22 13:31:01

回答

0

我已經找到一種方法,只顯示一個子類別的職位。

如果我在查詢中使用「category__in」,它將返回一個類別的帖子。

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

看起來你正在使用的例子使事情複雜化。如果您只需要來自特定類別的帖子,則可以使用類別slug或ID來檢索帖子,並使用WP_Query,如上例所示。因此,如果'Pop'類別的ID爲4(您可以通過將鼠標懸停在Posts - > Categories ...上的類別名稱中來獲取類別ID),那麼您的代碼將會是;

<?php 
$result = new WP_Query('cat=4'); 

while ($result->have_posts()) : $result->the_post(); 
?> 

以下是食典相關部分;

https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

相關問題