How to remove duplicate categories on a tag template? (1 post)

  1. Cathy Tibbles
    Posted 6 years ago #

    When a user clicks on a tag link, I want to show them a list of the tagged post's bottom-level categories(no descendants). (it sounds mixed up, but the bottom level cats are questions, the posts are each an answer to the question. I need to show the tagged posts' bottom level cat/question.

    This code lists the bottom-level cat for each post in the tag template, BUT it lists the bottom level cat for EACH post, so where there are more than one posts to a cat, it lists the cat multiple times.

    Is there a way to remove duplicates? (An example: http://new-dswp.com/tag/travel-with-infants-toddler/page/2/ )

           <?php if( have_posts() ) : while( have_posts() ): the_post();
            foreach((get_the_category()) as $this_cat) {
              if (get_category_children($this_cat->cat_ID) == "") {
                 $name = $this_cat->cat_name;
                 $link = get_category_link($this_cat->cat_ID);?>
    <li><a href="<?php echo $link ;?>" title="<?php echo $name;?>"><?php echo $name; ?></a></li>
    <?php } }?>
    <?php endwhile;?>
    <?php endif;?>

Topic Closed

This topic has been closed to new replies.

About this Topic