$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Get category's parent category while using get_the_category_list|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Get category's parent category while using get_the_category_list

matteradmin8PV0评论

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>';

}
Post a comment

comment list (0)

  1. No comments so far