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

categories - Get only children category name

programmeradmin2浏览0评论

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

        }
  }
发布评论

评论列表(0)

  1. 暂无评论