Forums

Listing Specific Category and Subcategories (3 posts)

  1. delvec28
    Member
    Posted 2 years ago #

    For my header navigation bar, I want to list ONE specific category, with a javascript/css drop-down menu of all the subcategories for that particular category.

    I've encountered a few problems though. When I use the following code, I only get the category, no subcategories.

    <?php $myNavCat = get_cat_ID($myNavCatName); ?>
    <ul>
    <?php wp_list_categories( 'title_li=&depth=0&include='.$myNavCat); ?>
    </ul>

    If I replace 'include' with 'child_of', I get all the subcategories, but not the parent category.

    Also, I've noticed that when you add a new post and select a subcategory, the parent is NOT selected by default. For instance, if I have a category called 'Animals', with a subcategory of 'Zebras', I want to select 'Zebras' and have it automatically placed within the Animals category.

    Any help with either of these issues is much appreciated!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    I think you'll need to use both. The include parameter only includes that one category - not its children.

    <?php $myNavCat = get_cat_ID($myNavCatName); ?>
    <ul>
    <?php wp_list_categories( 'title_li=&include='.$myNavCat);  // top level category ?>
    <?php wp_list_categories( 'title_li=&child_of='.$myNavCat); // children  ?>
    </ul>

    As for your category filing question, have you checked the generated category listings? Normally, Zebras posts would be listed under the Animals category on the public site if Zebras is a child of Animals. The connection in the Admin area is implicit rather than explicit.

  3. delvec28
    Member
    Posted 2 years ago #

    You are correct - it does list the inner category posts when I go to the parent category link.

    But if no post is explicitly checked off as the parent category, the wp_list_categories function will return 'no categories' instead of the category name.

Topic Closed

This topic has been closed to new replies.

About this Topic