Forums

Display current category in sidebar? (3 posts)

  1. ProTop
    Member
    Posted 2 years ago #

    Greetings all, this should be so simple but apparently not. I've searched high and low and I can't find the answer.

    When you are viewing a category (category.php) then how can you PHP echo or print or anything the current category?

    I'm not sure if this is an issue with the loop or what. I can't figure it out, I've tried like 25 code snippets.

    Cheers!

  2. alchymyth
    The Sweeper
    Posted 2 years ago #

    have you tried to use:
    <?php if(is_category()) { single_cat_title(); } ?>

    http://codex.wordpress.org/Template_Tags/single_cat_title

    this should probably work, and show the name of the current category in a category page; unless you have any plugins, or custom loops, that may distort the query.

  3. ProTop
    Member
    Posted 2 years ago #

    @alchymuth, thanks mate!

    I think I had tried that tag earlier, but somehow you jogged my brain into realizing I was trying to echo PHP WordPress tags.

    This is the final code I'm using in the sidebar, found in part at Yoast.com and including my small hack. It's useful for when you are browsing ANY type of category to list the subcategories in the sidebar. If there are no subcats, then this code defaults to showing the top used categories on your site. It requires further tweaking as per the codex if you want to only show 5 top categories, or etc.

    <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
    echo "<h2>Subcategories (";
    single_cat_title();
    echo ")</h2>";
    echo "<ul>";
    wp_list_categories('orderby=id&show_count=1&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID);
    echo "</ul>";
    }
    }
    else {
    echo "<h2>Top Categories</h2>";
    wp_list_categories('orderby=name&show_count=1&hierarchical=0&title_li=');
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic