admin管理员组文章数量:1317898
I want to display the direct category ancestor of a given post. An illustrative example:
These are the categories I have:
- Cat1
- Cat2
- Cat2.1
- Cat2.1.1
- Cat2.1.2
- Cat2.2
- Cat2.2.1
- Cat2.2.2.
- Cat2.3
- Cat2.1
- Cat3
- Cat4
I only put the posts in a single category between a same "level", but when the category has more sub categories, I check the whole trailing, like in:
- Cat1
- [X] Cat2
- [X] Cat2.1
- Cat2.1.1
- [X] Cat2.1.2
- Cat2.2
- Cat2.2.1
- Cat2.2.2.
- Cat2.3
- [X] Cat2.1
- Cat3
- Cat4
Now, in the single post page, I want to display the name of the direct category ancestor of the post (Cat2.1.2 in this case). By default I just use get_the_category()
, but it shows the top level category instead (Cat2 in this case). I don't considerer unchecking top levels by now because it cause another problems within the template.
I want to display the direct category ancestor of a given post. An illustrative example:
These are the categories I have:
- Cat1
- Cat2
- Cat2.1
- Cat2.1.1
- Cat2.1.2
- Cat2.2
- Cat2.2.1
- Cat2.2.2.
- Cat2.3
- Cat2.1
- Cat3
- Cat4
I only put the posts in a single category between a same "level", but when the category has more sub categories, I check the whole trailing, like in:
- Cat1
- [X] Cat2
- [X] Cat2.1
- Cat2.1.1
- [X] Cat2.1.2
- Cat2.2
- Cat2.2.1
- Cat2.2.2.
- Cat2.3
- [X] Cat2.1
- Cat3
- Cat4
Now, in the single post page, I want to display the name of the direct category ancestor of the post (Cat2.1.2 in this case). By default I just use get_the_category()
, but it shows the top level category instead (Cat2 in this case). I don't considerer unchecking top levels by now because it cause another problems within the template.
3 Answers
Reset to default 0I come to this post as i was looking for the code. But the marked answer not working for me anymore. I have wrote a function to get the deepest level category assigned to a post.
function post_deepest_level_cat($post_categories) {
foreach ($post_categories as $category) {
$cat_ids[] = $category->term_id;
}
$tree_args = array(
'current_category' => $cat_ids,
'depth' => 50,
'hierarchical' => true,
'echo' => 0,
);
$category_list = wp_list_categories($tree_args);
$dom = new DOMDocument;
@$dom->loadHTML($category_list);
$links = $dom->getElementsByTagName('a');
$new_cat_array = array();
foreach ($links as $link) {
$menu = get_term_by('name', $link->nodeValue, 'category');
if (in_array($menu->term_id, $cat_ids)) {
$deepest_cat_id = $menu->term_id;
}
}
return $deepest_cat_id;
}
And to use the above function here is the example.
$post_categories = wp_get_post_terms(10, 'category'); //All categories assigned to this post id "10"
$deeper_cat_id = post_deepest_level_cat($post_categories );
echo $deeper_cat_id; // will echo the id of deepest and last category of the current post.
This will help in 2020. If you have better code let me know. Thanks
It is possible that get_the_category()
will always return the categories in the correct order (with the deepest at the end), but if it doesn't, you could also loop over all the categories and remove those where another category points to it as their parent.
$post_categories = get_the_category();
$categories_by_id = array();
foreach ( $post_categories as $category ) {
$categories_by_id[$category->cat_ID] = $category;
}
foreach ( $post_categories as $category ) {
unset( $categories_by_id[$category->category_parent] );
}
// $categories_by_id will now only contain the deepest categories
Well, I guess I found how (not completely tested):
<?php
$category = get_the_category($post->ID);
echo end($category)->cat_name;
?>
Basically, it returns the last category name from the array of categories given by get_the_category
.
本文标签: categoriesDisplay only deepest category on a single post
版权声明:本文标题:categories - Display only deepest category on a single post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742036283a2417311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论