categories - Get category's parent category while using get_the_category_list

admin2025-06-02  2

I use get_the_category_list to create a cat link in the header of my posts in the archive page, like so :

$categories_list = get_the_category_list( esc_html__( ', ', 'mytheme' ) );
if ( $categories_list ) {
    echo '<div class="cat-links">';
    get_template_part('images/inline', 'picto-actu.svg');
    $categories_list . '</div>';
}

I am trying to include the parent category to show it like so on the frontend : parent category > the category . But I'm probably not using the right method because whatever i try, i can't seem to get the parent, i can't even get the id of the child category from $categories_list, which would help me find the parent...

I use get_the_category_list to create a cat link in the header of my posts in the archive page, like so :

$categories_list = get_the_category_list( esc_html__( ', ', 'mytheme' ) );
if ( $categories_list ) {
    echo '<div class="cat-links">';
    get_template_part('images/inline', 'picto-actu.svg');
    $categories_list . '</div>';
}

I am trying to include the parent category to show it like so on the frontend : parent category > the category . But I'm probably not using the right method because whatever i try, i can't seem to get the parent, i can't even get the id of the child category from $categories_list, which would help me find the parent...

Share Improve this question edited Mar 17, 2019 at 19:55 Qaisar Feroz 2,1471 gold badge9 silver badges20 bronze badges asked Mar 17, 2019 at 15:36 Xavier C.Xavier C. 577 bronze badges 1
  • The last statement should be echo $categories_list . '</div>'; – Qaisar Feroz Commented Mar 17, 2019 at 16:04
Add a comment  | 

2 Answers 2

Reset to default 1

get_the_category_list() returns a formatted list of category names, which does not (as you discovered) include the IDs.

get_the_category(), by contrast, returns an array of WP Term objects. A WP Term object looks like this:

 ["term_id"]=>  //int
    ["name"]=>   //string 
    ["slug"]=>  //string 
    ["term_group"]=>  //int
    ["term_taxonomy_id"]=> //int
    ["taxonomy"]=>   //string
    ["description"]=>    //string
    ["parent"]=> //int
    ["count"]=>  // int
    ["filter"]= //string
    ["meta"]= array(0) {} //array

Source: https://developer.wordpress/reference/classes/wp_term/

As you can see, you can very easily pull out both the ID and the parent ID if present.

Once you have the parent ID, you can fetch its name with get_cat_name( $cat_id );

Assuming that only one category is assigned to the post

$taxonomy = 'category';

// Get the term assigned to post.
$post_term = wp_get_object_terms( $post->ID, $taxonomy);

if ( ! empty( $post_term ) && ! is_wp_error( $post_term ) ) {

    // parent category object
    $parent = get_term($post_term[0]->parent, $taxonomy);

    echo '<div class="cat-links">';

    get_template_part('images/inline', 'picto-actu.svg');

    // Display post categories.
    echo '<a href="'. get_term_link($parent->term_id,$taxonomy).'">'.$parent->name.'</a> >';
    echo '<a href="'. get_term_link($post_term[0]->term_id,$taxonomy).'">'.$post_term[0]->name.'</a> >';

    echo '</div>';

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748798036a313804.html

最新回复(0)