0

I would love some help on how I can best display the custom taxonomy of a post as text only (and not as link within an tag).

I have a small template file that is called by a few other pages, and the job of that template file is to render a citation for an article - here's the complete code:

<div class="ehp_citation_block">
   <h6><strong><?php the_category(' | '); ?></strong></h6>
   <span class="alignright"><?php the_terms( get_the_ID(), 'ehp_volumes' ); ?></span>
   <?php the_field('ehp_citation'); ?>
   </div>

This portion of that php file:

<?php the_terms( get_the_ID(), 'ehp_volumes' ); ?>

Does its job in displaying the taxonomy - but it is wrapped in a hyperlink. Is there an attribute that will just return the text of taxonomy? Do I need to use a variable instead?

2

Use get_the_terms instead of the_terms.

http://codex.wordpress.org/Function_Reference/get_the_terms

Something like this should do the trick:

$terms = get_the_terms( get_the_ID(), 'ehp_volumes' );
$term_count = count( $terms );
if ( $term_count > 0 && ! wp_error( $terms ) ) {
  $terms = array_values( $terms ); // reset keys for easier looping. by default the key matches the term id
  for( $i = 0; $i <= $term_count; $i++ ) {
    $output.= $terms[$i]->name;
    $output.= ( $i < $term_count-1 ? ', ' : null ); // add commas between terms, but not to the last one
  }
  echo $output;
  unset( $output );
}
unset( $terms, $term_count );

Below is an updated suggestion based on comments (need ability to order terms DESC). To accomplish this use wp_get_object_terms instead of get_the_terms since it accepts a third parameter allowing you to filter and order the results.

I actually like this one better - much simpler.

$terms = wp_get_object_terms(
  $post->ID,
  'ehp_volumes',
  array(
    'orderby' => 'name',
    'order' => 'DESC',
    'fields' => 'names'
  )
);
echo ( ! is_wp_error( $terms ) ? implode( ', ', $terms ) : 'null' );
unset( $terms );
  • Joey - thanks! Would that code go inside a php statement? I'm not sure how to implement your solution. – Hal Atkins Jun 11 '14 at 17:46
  • I've updated the script on how to output the terms. This would replace what you have currently in span.alignright. – Joey Yax Jun 11 '14 at 18:00
  • Awesome. Just put that in, and it is working. If you care to continue to lead me along on this problem, the solution you crafted creates another issue. The template file is being called ten times by the page (I'm using it to display a set of results), so it adds to the $terms variable every time. Can I null out the variable at the beginning? – Hal Atkins Jun 11 '14 at 18:10
  • Ah, sounds like we need to clear the $term_count and $terms vars as you suspected. I've updated my original answer to reflect this. – Joey Yax Jun 11 '14 at 18:20
  • Awesome! Really appreciate the help, Joey. – Hal Atkins Jun 11 '14 at 19:34

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.