0

I'm struggling with displaying sub categories limited to only direct children of the parent on category page... I hope someone can help me with that :)

I have working code - when the current category is selected, it displays all its subcategories... but also including grand children and so on...

<?php
  $category_id = get_query_var( 'cat' ); // Get current catgory ID
  $category = get_term( $category_id, 'category' ); // Fetch category term object

  $parent = $category->parent ? $category->parent : 
  $category_id;

  $args = array(
     'show_count' => false,
     'hide_empty' => true,
     'title_li' => '',
     'show_option_none' => '',
     'echo' => false
     );

     // Show the children of parent category
     if ( $category->parent ) {
       $args['child_of'] = $category->parent;
       //$args['exclude'] = $category_id; // Don't display the current category in this list
    }
    else {
      $args['child_of'] = $category_id;
    }

    // Get the category list
    $categories_list = wp_list_categories( $args );
    if ( $categories_list ) {?>
      <div class="subcategory-wrapper">
        <ul class="menu">
          <?php echo $categories_list; ?>
        </ul>
      </div>
    <?php } ?>

How to display only direct children of current parent category?

What I see now with above code:

=== Current category page selected (parent) ===

  • Child 01

    • Grandchild 01
  • Child 02

    • Grand child 01

      • Grand grand child 01
  • Child 03

  • Child 04

    • Grand child 01

What I want to see (even if the parent category has grand children):

=== Current category page selected (parent) ===

  • Child 01

  • Child 02

  • Child 03

  • Child 04

UPDATE:

to hide grand sub categories, change the $args array to:

 $args = array(
 'show_count' => false,
 'hide_empty' => true,
 'depth'=>1,
 'title_li' => '',
 'show_option_none' => '',
 'echo' => false
 );

Another problem is that his code doesn't work on the post pages, but only on archive/category pages.

Is there a way to get the same result - show direct children of the parent category to which the post belongs - on the post page?

This is what I have now on the post page:

  • Parent category

= = = = = =

Post content here

= = = = = =

This is what I want to have on the post page ( -> I want to display direct children of parent category like the code above does...but unfortunately this code doesn't work on the post page):

  • Parent category

    • Child 01 (the post belongs to this subcategory)

    • Child 02 (the post doesn't belong to this subcategory, but it is displayed, because it's the child of the parent category)

    • Child 03 (the post doesn't belong to this subcategory, but it is displayed, because it's the child of the parent category) etc.

= = = = = =

Post content here

= = = = = =

Any ideas?

  • 1
    try to add to $args 'depth'=>1 – Shibi Dec 16 '17 at 23:54
1

In the single post you can get the parent category like this

$category = wp_get_object_terms(get_the_ID(), 'category', array('parent'=>0));

$args = array(
 'show_count' => false,
 'hide_empty' => true,
 'title_li' => '',
 'show_option_none' => '',
 'echo' => false,
 'depth' => 1
 );


$args['child_of'] = $category[0]->term_id;

// Get the category list
$categories_list = wp_list_categories( $args );
if ( $categories_list ) {?>
  <div class="subcategory-wrapper">
    <ul class="menu">
      <?php echo $categories_list; ?>
    </ul>
  </div>
<?php } ?>
  • Ah, I know what you mean, but I don't want to show only the parent of the post... I want to show all the subcategories of the parent category... I'll try to edit my question to make it more clear... – Anda Dec 17 '17 at 0:48
  • I meant just replace your $category = get_term( $category_id, 'category' ); with this all the rest is the same. you need to change the $category_id too. – Shibi Dec 17 '17 at 0:49
  • @Anda Check the answer now – Shibi Dec 17 '17 at 0:58
  • Perfect! It works like a charm! I noticed that your code works not only in the single post, but also on the archive/category pages... Is it ok to replace the previous code with your new code, or will it cause some errors? – Anda Dec 17 '17 at 1:12
  • On the archive/category pages the function get_the_ID() return the first post ID. You need to think of cases where there is more than 1 category parent. So sometimes Its work sometimes not. same about the single. if there is more than 1 as you see I used $category[0]->term_id to take the first one if there is more you get only the childs of the first parent. – Shibi Dec 17 '17 at 1:18

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.