I'm trying to display Terms comma separated along with hyperlinking each term with its slug however, the terms are not hyperlinked with the slug. Terms are coming comma separated but the slug isn't getting linked.

Let me know what I'm doing wrong.

Here is the code below:

<?php $terms = get_the_terms( $post->ID ,'policytype' );

                for($cat_count=0; $cat_count<count($terms); $cat_count++) {

                <a href="<?php echo site_url(); ?>/policytype/<?php  echo $term->slug;?>" class="link">
                <?php   echo $terms[$cat_count]->name; 
                if ($cat_count<count($terms)-1){
                        echo ', ';
                <?php  }?>

The comma separated term link list is available with:

the_terms( $post->ID, 'policytype', '', ', ', '' );

Check the dev docs for more info on this function.

  • So, I do not need to write the loop function? Just use the_terms() function? Thanks. Jul 21 '17 at 7:21
  • yes, don't need foreach with it
    – birgire
    Jul 21 '17 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.