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
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/
foreach(get_the_category() as $category){
echo ($category->parent?get_category($category->parent)->name.': ':'').$category->name.'</br>';
}
This worked!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.