Forums

List Child Categories of Current Category Page (6 posts)

  1. bzmillerboy
    Member
    Posted 1 year ago #

    I'm looking for a way to list (in the side bar) all categories of the current category page I'm on.

    Example
    http://dev.kliffnotesmarketing.com/category/cheatsheets/
    On this page the category cheatsheets has 6 child categories. When I'm on this page I want those six child (sub) categories to display in the side bar.

    Issue
    When I use wp_list_categories() tag it does list some categories but they are not exclusively the child categories of my current page. I'm not sure how it is pulling in those categories (they appear random).

    I can't use the include or exclude argument because I want it to automatically detect which parent category the user is viewing.

    http://codex.wordpress.org/Function_Reference/wp_list_categories

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    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>
  3. bzmillerboy
    Member
    Posted 1 year ago #

    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:
    http://dev.kliffnotesmarketing.com/category/cheatsheets/

  4. bzmillerboy
    Member
    Posted 1 year ago #

    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:
    http://dev.kliffnotesmarketing.com/category/cheatsheets/gurus/

    Thanks

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    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

  6. bzmillerboy
    Member
    Posted 1 year ago #

    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:
    http://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:
    http://dev.kliffnotesmarketing.com/category/tip-and-tricks/

    Thanks Again...I hope others find this as well!

Topic Closed

This topic has been closed to new replies.

About this Topic