如何在帖子循环中仅显示父类别(没有子类别)?
我有:
<span class="entry-category"><?php the_category(', '); ?></span>
我该怎么办?
答案 0 :(得分:1)
foreach (get_the_category() as $cat) {
$parent = get_category($cat->category_parent);
if (!get_category($cat->category_parent)) {
// do something with the category, like use it to create a new query_posts or something
}
}
答案 1 :(得分:1)
您可以获取类别并检查每个条目的parent
字段,如
<?php
$categories = get_the_category();
if($categories){
foreach($categories as $category) {
if ($category->parent < 1) {
// Your action here
}
}
}
?>