Support » Developing with WordPress » wp_list_categories include parent

  • I have the following structure:

    – Fruit
    – – Apples
    – – Pears
    – – Oranges

    and I want to list all of these in a unordered list but when I use the following:

    <?php wp_list_categories('orderby=id&current_category=0&use_desc_for_title=0&title_li&child_of=32'); ?>

    The children are listed but the parent isn’t.

    Is there a way to include the parent in the list as well? E.g.

    
    <ul>
    <li>Fruit</li>
    <li>Apples</li>
    <li>Pears</li>
    <li>Oranges</li>
    </ul>
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m guessing the 32 in child_of is the parent category, Fruit, correct?

    You might be able to get what you want by going a step back behind “Fruit”. Assuming “Fruit” is a top-level category, ditch the child_of argument to include “Fruit”. Then use exclude_tree for any top-level categories (and their descendants) that you want to exclude.

    Finally, you might need to add the depth argument to include the children of Fruit.

    Documentation here: https://developer.wordpress.org/reference/functions/wp_list_categories/#parameters

    Thanks for this. The only trouble with using the exclude_tree is if I added further top level categories in the future I would then need to go into the code and add the new ID to the exclude list. Is that correct? That’s fine if it were just me but if this is for a client then could be tricky.

    I hear ya.

    If you just need the links, without showing numbers of posts, you could:

    1. Just add a link for “Fruit”
    2. Use wp_list_categories() for child_of “Fruit”.

    Or: maybe get_terms() would help you get what you want.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_list_categories include parent’ is closed to new replies.