最新消息: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 only children category name

matteradmin7PV0评论

On a single post, I display the post's categories like this:

  $categories = get_the_category($id);

My category hierarchy is: cat1 parent of cat2, parent of cat3, etc.

I would like to display only children categories (from cat2 to catN). I tried to use get_category_by_slug($id,'cat1') with no results...

On a single post, I display the post's categories like this:

  $categories = get_the_category($id);

My category hierarchy is: cat1 parent of cat2, parent of cat3, etc.

I would like to display only children categories (from cat2 to catN). I tried to use get_category_by_slug($id,'cat1') with no results...

Share Improve this question edited Mar 22, 2019 at 18:21 supershivas asked Mar 22, 2019 at 18:13 supershivassupershivas 1035 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For this specific question, Try this

  $categories = get_the_category($id);
  foreach($categories as $category){
        if($category->parent != 0){

         // Display it here
         echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';

        }
  }

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far