1

Does anyone know a straightforward way to display a mixed list of linked and unlinked (custom) terms, dependent on whether there is more than one instance of a term being used?

i.e. I have an item that may have three or four terms within a taxonomy. Of these terms, two or three may have also been applied to other items, but one or more only to that item. I want to display a list of terms for the item where if the term is used for other items, it's a link to a list of these, but if the item is the only one with that term, it isn't a link. (Object being to not provide a link if that link will only show the one item already being viewed!)

Therefore I can't (I don't think) just use get_the_term_list because this provides a linked list of all terms, regardless of how many instances of a term there are. I've got something working using wp_get_object_terms where there's only one term from a custom taxonomy assigned to a post, but I need to do the same thing where there are multiple terms, some of which should be links and some not.

I'm sure it would be possible by working out some long and clever script, but I was wondering if anyone knows an easy way [ not that I'd object if someone has already worked out the long and complex script and can point me to it! :-) ] I guess an alternative would be a fairly complex script but defined as a function, so the script itself is only entered once and I can use something simple each time I need it, but learning how to create functions from scratch is a challenge I've not taught myself yet!

If it helps with understanding - i.e. I haven't explained myself as well as I thought! - you can see an example of the challenge here. The BP tag, for instance, links to a list of items tagged BP ... but the list consists only of the one item a visitor was already viewing. The surveys tag used further down, links to a list where the original item is only one of two with that term.

Any help / suggestions really appreciated! :-)

1 Answer 1

2

You can use the count property of the taxonomy term and if it is > 1, then show the term linked to archive.

// inside loop, get the terms of a custom taxonomy for the current post
$terms = get_the_terms( get_the_ID(), 'your_custom_taxonomy' ); 

// cycle the terms and display the name, linked to archive if term count is > 1
foreach ( $terms as $term ) {
  $link = $term->count > 1 ? get_term_link( $term, $term->taxonomy ) : FALSE;
  echo '<li>';
  if ( $link ) echo '<a href="' . $link . '">'; // open link if needed
  echo $term->name; // always show term name
  if ( $link ) echo '</a>'; // close link if needed
  echo '</li>';
}
1
  • Wow! Works and nowhere near as long as I was fearing! Had to tweak it a little to apply different styles depending on whether they're links or not, but basic approach is spot on. As I have lots of taxonomies with mixtures of things that should have a link and that shouldn't, I think this is going to be come my favourite bit of code for a while! Thanks everso :-)
    – Kevin4fm
    Jul 14, 2014 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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