WordPress - 仅显示父类别帖子而非子类别帖子

时间:2013-04-22 05:40:42

标签: php wordpress mysqli

我一直在努力从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();
 ?> 

1 个答案:

答案 0 :(得分:0)

尝试使用此查询:

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