最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Correctly order the hierarchy of custom taxonomy when displaying terms

programmeradmin6浏览0评论

On my page I am displaying the custom taxonomy terms relevant to a post. For example a post may have a heirachy of:

Filmmaking -> Production -> Cinematogrpahy

To do this I am using the below code in my content-child page

<?php
    /*Retrieve Category Name */ 
        if(get_the_terms($post->ID, 'kernal_category', true)) {
            // Create an empty array

            $kernal_category = get_the_terms($post->ID, 'kernal_category', true);

            $categories = [];

            // Save each category to the array
            foreach ($kernal_category as $category) {

            $categories[] = $category->name;
            }

            // Now output with implode
                        echo implode(' > ', $categories);
            }
    ?>

My problem is that when these terms are displayed they are not following the correct hierachy. Using the example above my post is actually displayed as follows: Cinematography -> Production -> Filmmaking.

I have already checked and the primary for this taxonomy is set correctly i.e. the Primary for Cinematography is Filmmaking

How do I display them hierarchically?

Update: It appears to affect taxonomies with more than a Primary and Secondary term. All posts with just primary and secondary are displaying correctly.

On my page I am displaying the custom taxonomy terms relevant to a post. For example a post may have a heirachy of:

Filmmaking -> Production -> Cinematogrpahy

To do this I am using the below code in my content-child page

<?php
    /*Retrieve Category Name */ 
        if(get_the_terms($post->ID, 'kernal_category', true)) {
            // Create an empty array

            $kernal_category = get_the_terms($post->ID, 'kernal_category', true);

            $categories = [];

            // Save each category to the array
            foreach ($kernal_category as $category) {

            $categories[] = $category->name;
            }

            // Now output with implode
                        echo implode(' > ', $categories);
            }
    ?>

My problem is that when these terms are displayed they are not following the correct hierachy. Using the example above my post is actually displayed as follows: Cinematography -> Production -> Filmmaking.

I have already checked and the primary for this taxonomy is set correctly i.e. the Primary for Cinematography is Filmmaking

How do I display them hierarchically?

Update: It appears to affect taxonomies with more than a Primary and Secondary term. All posts with just primary and secondary are displaying correctly.

Share Improve this question asked Feb 5, 2020 at 17:37 JamesJames 1191 silver badge5 bronze badges 5
  • There's an important distinction that needs to be known to answer this question, depending on how your understand terms to work. Did you check Cinematography? Or did you check all 3 terms Cinematography Production and Film making? If it's the first one, then a really simple solution can be written as the answer, but if it's the second one... the answer will have to be more complex due to the misunderstanding. Finally, are all terms guaranteed to be in a parent/child/grandchild/etc relationship? Is this an attempt to build breadcrumbs? – Tom J Nowell Commented Feb 5, 2020 at 17:59
  • @TomJNowell apologies for the omission. I have ticked all 3, Filmmaking, Production, Cinematography. And yes all cases will be parent/child/grandchild. I will never for example be clicking 2 parent, or 2 child terms for one post. It is a breadcrumb, it is to let a user know how deep into a particular taxonomy the post they are viewing is. – James Commented Feb 5, 2020 at 18:10
  • 1 Ah, that complicates things. For future reference, you only need to assign the grandchild term, ticking the parent terms is unnecessary. It's a bit like saying something is a sweater, then explicitly stating that it is also an item of clothing ( where clothing is the parent term ), it's just generally understood that sweaters are clothing, terms are inclusive and contain all the things assigned to their child terms too unless otherwise stated – Tom J Nowell Commented Feb 5, 2020 at 18:46
  • Thanks for the advice, I did however try this and when displaying the breadcrumb all that is shown now is the grandchild term. Is there though now an easy way of coding the function to show the parent terms? I am only using test data so far. – James Commented Feb 5, 2020 at 21:06
  • 1 You have to manually account for them, as I said, the solution for either circumstance is different – Tom J Nowell Commented Feb 5, 2020 at 21:29
Add a comment  | 

1 Answer 1

Reset to default 1

If you want to show a term and all its parents/ancestors, you can do the following:

function display_term_parents( \WP_Term $starting_term ) {
    $terms = [];
    $term = $starting_term;
    while ( !is_wp_error( $term ) ) {
        $terms[] = $term->name;
        $term = get_term( $term->parent, 'kernal_category' );
    }
    $terms = array_reverse( $terms );
    echo implode(' > ', $terms );
}

Where $starting_term is the term you want to show the ancestors/parents for.

This can be done for each term ( or just the first term ) that a post has, assuming that you've only checked the term you want, but not all of its parents too

发布评论

评论列表(0)

  1. 暂无评论