categories - Get only children category name

On a single post,I display the post's categories like this:$categories = get_the_category($id);My category hierar

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

        }
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664862a4639061.html

相关推荐

  • categories - Get only children category name

    On a single post,I display the post's categories like this:$categories = get_the_category($id);My category hierar

    21天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信