WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show Sub-Categories When On Parent Category Page (9 posts)

  1. TriLeafDesigns
    Member
    Posted 3 years ago #

    http://trileafdesigns.com/calendar/

    Ok, so my set up is that my navigation is all the parent categories. I want to be able to click one of the parent categories and then their sub categories show up in the gray bar below the navigation. I don't want a drop down and I don't want it to show all sub categories of all parent categories. Not sure how to accomplish this task though. Link to the page is at the top.

    Can any of you guys help me out with this one?

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Use wp_list_categories with the child_of parameter?

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    <ul>
    <?php if( is_category() ) {
    	$cat_id = get_query_var('cat');
    	$cat_ancestors = array();
    	$cat_ancestors[] = $cat_id;
    	do {
    	$cat_id = get_category($cat_id);
    	$cat_id = $cat_id->parent;
    	$cat_ancestors[] = $cat_id; }
    	while ($cat_id);
    	$cat_ancestors = array_reverse( $cat_ancestors );
    	$top_cat = $cat_ancestors[1];
    wp_list_categories('title_li=&child_of=' . $top_cat);
    }
    	?>
    </ul>

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

    most of the code is to make sure that the menu stays even if you go deeper into the child categories.

    styling this into a horizontal menu is up to you.

  4. TriLeafDesigns
    Member
    Posted 3 years ago #

    Well it shows the dropdown menu. Is this javascript? Also it says "Categories" then jumps into a dropdown menu of categories I don't have. Any suggestions on how to fix this?

  5. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    Well it shows the dropdown menu. Is this javascript?

    the 'test1' under 'picture perfect' looks like javascript dropdown; probably already part of your theme.

    Also it says "Categories"

    what code have you used?

    please paste the full code of your menu into a http://wordpress.pastebin.com/ and post the link to it here.

    then jumps into a dropdown menu of categories I don't have.

    i don't see any other categories than the ones you already have in your horizontal list.

    btw:
    is 'calendar' alreday a category?
    does this mean you are planning to do this menu on the second level of categories?

  6. TriLeafDesigns
    Member
    Posted 3 years ago #

  7. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    thanks,

    as far as i can see, the code is working as expected.

    as you have no posts with any child-categories, it is showing nothing.

    you could add hide_empty=0 to this line, as shown below, to have the empty categories shown as well:
    wp_list_categories('title_li=&hide_empty=0&child_of=' . $top_cat);

    also, to suppress the 'No categories' message, you could add another parameter:
    wp_list_categories('title_li=&hide_empty=0&show_option_none=&child_of=' . $top_cat);

    to prevent dropdowns from your top-category list, you might add depth=1 to this line, as below:

    <li><?php wp_list_categories('hide_empty=0&orderby=slug&show_count=0&depth=1&title_li='); ?></li>

    btw:
    http://codex.wordpress.org/Creating_Horizontal_Menus

  8. TriLeafDesigns
    Member
    Posted 3 years ago #

    :D !

    Dude your a genius! That works perfectly! Thanks man much appreciated.

    Thanks again!

  9. Audee
    Member
    Posted 3 years ago #

    Hello everyone :)
    I follow this and try to integrate it with a 'category icons' plugin.
    I have a question though, anyone knows how to include the sub-category description?
    Here's what I have so far:

    <ul class="featprod-item">
    <?php if( is_category() ) {
    	$cat_id = get_query_var('cat');
    	$cat_ancestors = array();
    	$cat_ancestors[] = $cat_id;
    	do {
    	$cat_id = get_category($cat_id);
    	$cat_id = $cat_id->parent;
    	$cat_ancestors[] = $cat_id; }
    	while ($cat_id);
    	$cat_ancestors = array_reverse( $cat_ancestors );
    	$top_cat = $cat_ancestors[1];
    if (function_exists('put_cat_icons')) put_cat_icons( wp_list_categories('use_desc_for_title=1&orderby=name&title_li=&child_of=' . $top_cat.'&echo=0'));
    else wp_list_categories('use_desc_for_title=1&orderby=name&echo category_description()&title_li=&child_of=' . $top_cat);
    }
    	?>
    
    </ul>

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic