WordPress-仅显示父类别帖子,不显示子类别帖子 [英] WordPress - Display only Parent Category posts not sub categories posts

查看:99
本文介绍了WordPress-仅显示父类别帖子,不显示子类别帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力从Wordpress中提取父类别帖子.当我尝试显示唯一的父类别帖子时,wordpress查询也会显示子类别帖子.

I have been struggling to extract the parent category posts from Wordpress. When i am trying to display the only parent category posts the wordpress query displaying the sub-categories posts as well.

如何省略子类别的帖子.请帮助!..

How can omit the sub-category posts. please help!!..

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

推荐答案

尝试使用此查询:

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

这篇关于WordPress-仅显示父类别帖子,不显示子类别帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆