[英]Display category parents and their children
I would like to display all the categories related to a post, along with their parents.我想显示与帖子相关的所有类别及其父母。
Currently I have something like this: Dog, Female, London, Newborn目前我有这样的东西:狗,女性,伦敦,新生儿
I'm using this code:我正在使用这段代码:
$categories_list = get_the_category_list( __( '</br> ', 'twentytwentyone' ) );
if ( $categories_list ) {
printf(
'<p class="cat-links"> ' . ( $categories_list ) . ' </p></div>'
);
}
But I would like to also display the parent category as such:但我也想这样显示父类别:
<-----------> <----------->
Type: Dog类型:狗
Gender: Female性别女
Location: London地点:伦敦
Age: Newborn年龄:新生儿
<-----------> <----------->
Note: I have tried simply writing HTML to group these as such注意:我尝试简单地编写 HTML 来将这些分组
Thank you in advance!先感谢您!
try this:尝试这个:
foreach(get_categories() as $category){
echo ($category->parent?get_category($category->parent)->name.': ':'').$category->name.'</br>';
}
https://developer.wordpress.org/reference/functions/get_categories/ https://developer.wordpress.org/reference/functions/get_category/ https://developer.wordpress.org/reference/functions/get_categories/ https://developer.wordpress.org/reference/functions/get_category/
foreach(get_the_category() as $category){
echo ($category->parent?get_category($category->parent)->name.': ':'').$category->name.'</br>';
}
This worked!这行得通!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.