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

taxonomy - Get all sub-categories of a parent category

programmeradmin1浏览0评论

How could I get a list of sub-categories (empty included) of a specific parent category (not within a post) by providing the parent category ID or slug? I need 3 levels depth. Example: picture

My code so far:

function smart_category_top_parent_id ($catid) {
    while ($catid) {
       $cat = get_category($catid); // get the object for the catid
       $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
      // the while loop will continue whilst there is a $catid
      // when there is no longer a parent $catid will be NULL so we can assign our $catParent
       $catParent = $cat->cat_ID;
    }
return $catParent;

$page_id = get_queried_object_id();

$category = get_the_category($page_id);

$catid = $category[0]->cat_ID;

$top_level_cat = smart_category_top_parent_id ($catid);

$post_terms = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );

$terms = wp_list_categories( array(
    'title_li' => '',
    'style'    => 'none',
    'echo'     => false,
    'taxonomy' => 'category',
    'include'  => $top_level_cat,
    'show_count' => 1,
    'child_of' => 0,
    'separator' => '<br />',
    'style' => 'list',
    'hide_empty' => 0,
    'include' => $post_terms
   ) );

   return $terms;

Code above displays only the top parent category but not children.

How could I get a list of sub-categories (empty included) of a specific parent category (not within a post) by providing the parent category ID or slug? I need 3 levels depth. Example: picture

My code so far:

function smart_category_top_parent_id ($catid) {
    while ($catid) {
       $cat = get_category($catid); // get the object for the catid
       $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
      // the while loop will continue whilst there is a $catid
      // when there is no longer a parent $catid will be NULL so we can assign our $catParent
       $catParent = $cat->cat_ID;
    }
return $catParent;

$page_id = get_queried_object_id();

$category = get_the_category($page_id);

$catid = $category[0]->cat_ID;

$top_level_cat = smart_category_top_parent_id ($catid);

$post_terms = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );

$terms = wp_list_categories( array(
    'title_li' => '',
    'style'    => 'none',
    'echo'     => false,
    'taxonomy' => 'category',
    'include'  => $top_level_cat,
    'show_count' => 1,
    'child_of' => 0,
    'separator' => '<br />',
    'style' => 'list',
    'hide_empty' => 0,
    'include' => $post_terms
   ) );

   return $terms;

Code above displays only the top parent category but not children.

Share Improve this question edited Mar 9, 2020 at 22:31 Arg Geo asked Mar 9, 2020 at 18:12 Arg GeoArg Geo 4591 gold badge8 silver badges20 bronze badges 3
  • Have you tried wp_list_categories()? – Sally CJ Commented Mar 9, 2020 at 18:55
  • Yeap, I have, after reading the documentation but couldn't get it to work. – Arg Geo Commented Mar 9, 2020 at 19:02
  • @SallyCJ added my code – Arg Geo Commented Mar 9, 2020 at 19:11
Add a comment  | 

1 Answer 1

Reset to default 2

In reply to your updated code, you should know that the $post_terms = wp_get_object_terms() would only return categories that directly attached to the post, so I'd just use wp_list_categories() to get and display the child categories for the given (or a known) parent category.

And here's the code that works well for me which displays up to three levels of child categories including those having no posts. This code would replace the lines starting from the $page_id = get_queried_object_id(); to the $terms = wp_list_categories(); as in the question, and I'm using get_ancestors() instead of (your) smart_category_top_parent_id():

$page_id = get_queried_object_id();

$category = get_the_category( $page_id );
$catid = ( ! empty( $category ) ) ? $category[0]->cat_ID : 0;

if ( $catid ) {
    // Get all parent IDs.
    $parents = get_ancestors( $catid, 'category', 'taxonomy' );

    $top_level_cat = ( count( $parents ) > 1 ) ?
        $parents[ count( $parents ) - 2 ] : // use the second parent category ID, or
        array_pop( $parents );              // otherwise, we'll use the first one

    $terms = wp_list_categories( array(
        'title_li'   => '',
        'echo'       => false,
        'taxonomy'   => 'category',
        'show_count' => 1,
        'child_of'   => $top_level_cat,
        'style'      => 'list',
        'hide_empty' => 0, // include empty categories
        'depth'      => 3, // and up to 3 levels depth
    ) );
}
发布评论

评论列表(0)

  1. 暂无评论