• Hi,

    I’m trying to get the child categories of a chosen parent to display- in the loop.

    For example, imagine there are two parent-categories called ‘Colour’ and ‘Shape’, with child-categories like ‘Red’ or ‘Green’ and ‘Square’ or ‘Triangle’. I want each post to display these child categories in specific places on my template, so it’s easy to see the Colour and Shape of each post, without cluttering it up with any other categories etc.

    But it’s proving a little tough, as the_category doesn’t seem to allow for exclusions etc in the same way as wp_list_categories– which doesn’t work inside the loop, obviously.

    So I’ve been digging around the template tags and I think in_category is on the right track, but I’m not at all hot with php so I can’t really see a way to make it spit out the children, so to speak.

    Any help anyone can offer would be hugely fantastic, even just a clue.

    Thanks!

Viewing 2 replies - 16 through 17 (of 17 total)
  • if you are trying to do this on a single.php there is a much easier way. in the loop, simply put

    <?php the_category(' / ',',single,') ?>

    where “/” is your deliniator and “single” tells wordpress to only collect the children. the other option for this is “multiple” (which pulls both parent & child). so if you wanted to display items in the parent category “ingredients” by listing them with a comma, you would insert

    <?php the_category(' , ',',single,') ?>

    or if you wanted to list all the post tags including parent separated by a “>” you would write

    <?php the_category(' > ',',multiple,') ?>

    this only works for single categories if there is only ONE parent/child category chosen for you post. if you had tagged your post with the parent category “chicken” and “recipes,” both parents would show. but using the ‘single’ callout, it would show only the children of “chicken” and “recipes”

    Using that code:

    <?php foreach((get_the_category(‘orderby=ID&order=ASC’)) as $childcat) {if (cat_is_ancestor_of(8, $childcat)) {echo ‘cat_ID).'”>’;
    echo $childcat->cat_name . ‘
    ? ‘;}}?></span>

    How can I make to short it by ID ??
    I am trying with
    ..get_the_category(‘orderby=ID&order=DESC’)…
    but it doesnt work!

    Thanks!

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘How to display the child category of a specific parent category…’ is closed to new replies.