Forums

Displaying multiple child categories of different parents (3 posts)

  1. dianat
    Member
    Posted 3 months ago #

    I need to be able to display multiple child categories with links, like this (with free text prepended).

    Color: Red
    Shape: Circle
    Size: Large

    I found a great solution at http://wordpress.org/support/topic/how-to-display-the-child-category-of-a-specific-parent-category?replies=18, which I've tweaked to include the prepended text.

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(10, $childcat)) {
    echo 'Color: ' . '<a href="'.get_category_link($childcat->cat_ID).'">';
    echo $childcat->cat_name . '</a>';
    }}
    ?>

    However, I'm unable to get multiple iterations to work. I tried adding and endif; to each with no joy.

    How can I get this to work?

    Thanks.

    Diana

  2. dianat
    Member
    Posted 3 months ago #

    Ah, I just had to nest them properly:

    <span class="cat-links">
    	<?php
    	foreach((get_the_category()) as $childcat) {
    	if (cat_is_ancestor_of(3, $childcat)) {
    	echo 'Color: '. '<a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name . '</a>';
    	}
    	if (cat_is_ancestor_of(10, $childcat)) {
    	echo 'Shape: '. '<a href="'.get_category_link($childcat->cat_ID).'">';
     echo $childcat->cat_name . '</a>';
    	}
    	}
    	?>
    </span>
    <?php endif; // End if categories ?>
  3. dianat
    Member
    Posted 3 months ago #

    Last step, need them displayed in parent ID order. By default, content is displayed by Child alpha order. Changed get_the_category() to get_the_category('orderby=ID&order=ASC'). No joy.

    Any ideas?

Reply

You must log in to post.

About this Topic