Viewing 5 replies - 1 through 5 (of 5 total)
  • When I use wp_list_categories() tag it does list some categories but they are not exclusively the child categories of my current page.

    what is thew exact code you were using?

    you could try:

    <ul>
    <?php
    $wp_reset_query(); // just in case the original query_string got disturbed
    $this_cat = get_query_var('cat'); // get the category of this category archive page
    wp_list_categories('child_of=' . $this_cat . '&title_li='); // list child categories
    ?>
    </ul>

    Thread Starter bzmillerboy

    (@bzmillerboy)

    Thanks, that appears to work however for some reason it is only listing the first and only one sub-category.

    Here is my code:

    <h2>get_query_var('cat')</h2>
    <ul>
    <?php $this_cat = (get_query_var('cat')) ? get_query_var('cat') : 1; ?>
    <?php wp_list_categories('child_of=' . $this_cat . ''); ?>
    </ul>
    
    <?php echo "Results restricted to category = $this_cat"; ?>
    <h2>wp_list_categories('child_of=19')</h2>
    <ul>
    <?php wp_list_categories('child_of=19'); ?>
    </ul>

    Here is an example:
    https://dev.kliffnotesmarketing.com/category/cheatsheets/

    Thread Starter bzmillerboy

    (@bzmillerboy)

    Sorry, found my issue.

    I didn’t have post assigned to those categories so they were not showing up. It didn’t occur to me that the category would not display if there were no post in that category…but that makes sense.

    Next Challenge:
    I’ll have 2 levels of categories so how do I show the categories of the firs level even when on a second level category page. Currently if I’m on a 2nd level category page the above code outputs “No Categories”…which makes sense because my 2nd level categories don’t have child categories.

    Example:
    https://dev.kliffnotesmarketing.com/category/cheatsheets/gurus/

    Thanks

    try something like:

    <?php $this_cat = (get_query_var('cat')) ? get_query_var('cat') : 1; ?>
    <?php $this_category = get_category($this_cat);
    if ( $this_category->parent ) { $this_cat = $this_category->parent; } ?>
    <?php wp_list_categories('child_of=' . $this_cat . ''); ?>

    this will obviously only work if there are just two levels of categories

    Thread Starter bzmillerboy

    (@bzmillerboy)

    Thank You a million…this works perfect. I think this really extends the use of WordPress and I’m surprised I didn’t find it used more often.

    Check it out:
    https://dev.kliffnotesmarketing.com/category/cheatsheets/

    I also extended it a little further. This way for a parent category that has no child/sub category…the text “no categories” will not display.

    function bm_dont_display_it($content) {
      if (!empty($content)) {
        $content = str_ireplace('<h2>Categories</h2>' . '<ul>' . '<li>' .__( "No categories" ). '</li>' . '</ul>', "", $content);
      }
      return $content;
    }
    add_filter('wp_list_categories','bm_dont_display_it');

    For example, this category has no child/sub categories:
    https://dev.kliffnotesmarketing.com/category/tip-and-tricks/

    Thanks Again…I hope others find this as well!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘List Child Categories of Current Category Page’ is closed to new replies.