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.

  • Have you tried wp_list_categories()? – Sally CJ Mar 9 at 18:55
  • Yeap, I have, after reading the documentation but couldn't get it to work. – Arg Geo Mar 9 at 19:02
  • @SallyCJ added my code – Arg Geo Mar 9 at 19:11
1

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
    ) );
}
| improve this answer | |
  • Thank you, unfortunately I do not get the desired results. I get partial results. – Arg Geo Mar 9 at 19:42
  • I added code to the answer. Is that how you attempted it? – Sally CJ Mar 9 at 19:55
  • Yeap, I think it does, at least it seems so, I will verify later and mark the answer as correct. I think it worked before, but maybe lagged, I see you just removed some bits, am I right? – Arg Geo Mar 9 at 20:51
  • Yeap it does work, however could you please tell me how to replace $top_level_cat (which is the top parent) with his first direct child (to which the post belongs to)? Thank you. – Arg Geo Mar 9 at 21:43
  • 1
    You are great, works like a charm! Thank you so much! – Arg Geo Mar 10 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.