Wordpress: Comma separated list of taxonomy terms


/ Published in: PHP
Save to your folder(s)

Create a comma separated list of a post's custom taxonomy terms.


Copy this code and paste it in your HTML
  1. <?php
  2. // Get a list of terms for this post's custom taxonomy.
  3. $project_cats = get_the_terms($post->ID, 'TAXONOMY_NAME');
  4. // Renumber array.
  5. $project_cats = array_values($project_cats);
  6. for($cat_count=0; $cat_count<count($project_cats); $cat_count++) {
  7. // Each array item is an object. Display its 'name' value.
  8. echo $project_cats[$cat_count]->name;
  9. // If there is more than one term, comma separate them.
  10. if ($cat_count<count($project_cats)-1){
  11. echo ', ';
  12. }
  13. }
  14. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.