WordPress.org

Support

Support » How-To and Troubleshooting » Show subcategories only when browsing category

Show subcategories only when browsing category

  • Is there any way/code for WordPress to show only the parent categories when browsing the main page/site yet when a category is clicked/browsed, show the subcategories under it?

    Okay, rather confusing question. Let me rephrase with a scenario type.

    Say I have categories A, B, and C. These categories are displayed on the side bar at all time.

    Now when I browsing category A, the subcategories under it (a-a, a-b) would then appear.

    If I went to category B, then the subcategories for A would dissapear, and the subcategories (if any) for B would appear.

    Hope I made that clear enough. Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’ve been looking for a way to do this as well. I keep searching and finding hints at it, but nothing that’s got me all the way there. So far, I’m settling for css drop down menus, but I’d really like to show the full sub-cats when in one.

    Controlling the category page:
    Have you tried to use a custom category.php template? This will display the parent category, and all of its children. (check the default theme installation, and copy into your theme folder; that one displays titles and excerpts, which you can change with template tags).
    http://codex.wordpress.org/Category_Templates
    http://codex.wordpress.org/Template_Tags/

    I’m stumped on the display for parents only on homepage, but gimme a sec, and i’ll see if i can get something.

    seems like it should be easy!

    Controlling the front page: (home.php)

    wow, the best i could see is hard-coding the home.php with an if/else using ‘in_category’… this is goes within the Loop, so it tests for each entry.
    http://codex.wordpress.org/Template_Tags/in_category

    this is untested, but might help you:

    <!-- Start the Loop. -->
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <!-- test for ID of your parent categories, adding more with the 'or' symbol '||' etc -->
    <?php if ( in_category('5') || in_category('8') || in_category('9') ) { ?>

    <!-- put your display stuff here i put some basic things below:-->

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

    <small><?php the_time('F jS, Y'); ?></small>

    <?php the_content(); ?>

    <?php } ?>

    <!-- end the loop -->
    <?php endwhile; else: ?>
    <?php endif; ?>

    it’s no fun hard-coding things, since you know you have to edit the template later. maybe you’ll find a better solution. please come back and post it here!

    Oh wow. Thank you thank you thank you! I’ll test it to see if it works.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show subcategories only when browsing category’ is closed to new replies.
Skip to toolbar