Posted By

lromak on 09/12/13


Tagged

wordpress


Versions (?)

List current post taxonomies and terms


 / Published in: PHP
 

To be used in WordPress template. List full taxonomy, or only custom post type associated with the post. Examples include a linking or non linking list.

  1. //To list FULL custom taxonomy
  2. <?php
  3. $taxonomy = 'replace_this_with_taxonomy_name';
  4. $queried_term = get_query_var($taxonomy);
  5. $terms = get_terms($taxonomy, 'slug='.$queried_term);
  6. if ($terms) {
  7. echo '<ul>';
  8. foreach($terms as $term) {
  9. echo '<li><a href="'.get_term_link($term->slug, $taxonomy).'">'.$term->name.'</a></li>';
  10. }
  11. echo '</ul>';
  12. }
  13. ?>
  14.  
  15. //To list ONLY custom taxonomy associated with post:
  16.  
  17. //non-linking, list only:
  18.  
  19. <?php $terms = wp_get_post_terms($post->ID,'replace_this_with_taxonomy_name');
  20. $count = count($terms);
  21. if ( $count > 0 ){
  22. echo "<ul>";
  23. foreach ( $terms as $term ) {
  24. echo '<li><a href="'.get_term_link($term->slug, 'replace_this_with_taxonomy_name').'">'. $term->name . "</a></li>";
  25. }
  26. echo "</ul>";
  27. }?>
  28.  
  29. //links to taxonomy archive:
  30.  
  31. <?php $terms = wp_get_post_terms($post->ID,'replace_this_with_taxonomy_name');
  32. $count = count($terms);
  33. if ( $count > 0 ){
  34. echo "<ul>";
  35. foreach ( $terms as $term ) {
  36. echo '<li><a href="'.get_term_link($term->slug, 'replace_this_with_taxonomy_name').'">'. $term->name . "</a></li>";
  37. }
  38. echo "</ul>";
  39. }?>

Report this snippet  

You need to login to post a comment.