简体   繁体   English

显示类别父母和他们的孩子

[英]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 来将这些分组

  • Type类型
  • Gender性别
  • Location地点
  • Age年龄
  • This doesn't work because the categories listed are not ordered, therefore sometimes the output will be something like (Gender: Dog, Location: Female,Type: London) 这不起作用,因为列出的类别没有排序,因此有时 output 将类似于(性别:狗,位置:女性,类型:伦敦)

    Thank you in advance!先感谢您!

    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.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM