[英]WordPress - Display only Parent Category posts not sub categories posts
我一直在努力从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();
?>
尝试使用以下查询:
$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.