admin管理员组文章数量:1317915
I'm trying to show the only the last child terms of a taxonomy in a post.
For example, the post "Johnny Pastafrolla" has the following terms of the taxonomy "camp" selected:
- Summer Camp
- Summer Camp 2018
- Summer Camp 2019
- Space Camp
- Winter Camp
- Winter Camp 2017
In this case, the displayed terms are gonna be: Summer Camp 2018, Summer Camp 2019, Space Camp, Winter Camp 2017
I found a code online which is doing this, but for Categories.
add_filter( 'the_category_list', 'ci_theme_the_category_list_remove_parent_categories', 10 );
function ci_theme_the_category_list_remove_parent_categories( $categories ) {
$categories_tmp = $categories;
foreach ( $categories_tmp as $child_cat ) {
foreach ( $categories_tmp as $key => $parent_cat ) {
if ( isset( $categories[ $key ] ) ) {
if ( cat_is_ancestor_of( $parent_cat, $child_cat ) ) {
unset( $categories[ $key ] );
}
}
}
}
return $categories;
}
I'm trying to "adapt it" for this specific taxonomy, but I'm kind of lost. Any hint?
Thank you
Dave
I'm trying to show the only the last child terms of a taxonomy in a post.
For example, the post "Johnny Pastafrolla" has the following terms of the taxonomy "camp" selected:
- Summer Camp
- Summer Camp 2018
- Summer Camp 2019
- Space Camp
- Winter Camp
- Winter Camp 2017
In this case, the displayed terms are gonna be: Summer Camp 2018, Summer Camp 2019, Space Camp, Winter Camp 2017
I found a code online which is doing this, but for Categories.
add_filter( 'the_category_list', 'ci_theme_the_category_list_remove_parent_categories', 10 );
function ci_theme_the_category_list_remove_parent_categories( $categories ) {
$categories_tmp = $categories;
foreach ( $categories_tmp as $child_cat ) {
foreach ( $categories_tmp as $key => $parent_cat ) {
if ( isset( $categories[ $key ] ) ) {
if ( cat_is_ancestor_of( $parent_cat, $child_cat ) ) {
unset( $categories[ $key ] );
}
}
}
}
return $categories;
}
I'm trying to "adapt it" for this specific taxonomy, but I'm kind of lost. Any hint?
Thank you
Dave
Share Improve this question edited Oct 31, 2020 at 0:52 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Oct 31, 2020 at 0:07 DandeDande 351 silver badge8 bronze badges 3 |1 Answer
Reset to default 0I wanted to display the terms with the get_the_term_list function so, since categories & taxonomy have a "similar logic", I replaced the "the_category_list"
add_filter( 'the_category_list', 'ci_theme_the_category_list_remove_parent_categories', 10 );
with "get_the_terms"
add_filter( 'get_the_terms', 'only_last_taxonomy_terms', 10 );
and it does what I need.
本文标签: how to show only last child terms of a taxonomy
版权声明:本文标题:how to show only last child terms of a taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742024134a2415224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Winter Camp 2017
orWinter Camp
– Tom J Nowell ♦ Commented Oct 31, 2020 at 0:51