我试图在一篇文章中展示分类法的最后一个术语。
例如,“约翰尼·帕斯塔夫洛拉”一文中选择了以下分类“阵营”术语:
在这种情况下,显示的术语将是:2018年夏令营,2019年夏令营,2017年冬季夏令营。
我找到了一个代码在线,它正在执行此操作,但用于类别。
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;
}我试图“调整它”以适应这个特定的分类法,但我有点迷失了。有什么暗示吗?
谢谢
戴夫
发布于 2020-10-31 13:29:48
我想用get_the_term_list函数显示术语,因此,由于类别和分类法具有“相似的逻辑”,所以我替换了"the_category_list“
add_filter( 'the_category_list', 'ci_theme_the_category_list_remove_parent_categories', 10 );
加上"get_the_terms“
add_filter( 'get_the_terms', 'only_last_taxonomy_terms', 10 );
它能满足我的需要。
https://wordpress.stackexchange.com/questions/377430
复制相似问题