Your Ad Here

Posted By

PubliOne on 02/15/11


Tagged

wordpress function


Versions (?)

has_term() function


 / Published in: PHP
 

  1. <?php
  2. function has_term($term_name, $taxonomy_name) {
  3. $terms = get_terms($taxonomy_name, 'fields=names');
  4. for ($i=0; $i < count($terms); $i++) {
  5. if ($terms[$i] == $term_name) {
  6. return true;
  7. }
  8. } return false;
  9. }
  10. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: PubliOne on February 15, 2011

Ok, you created a Custom Post Type and some Custom Taxonomies for it. Now you want to check wether your post has a term (aka category) from the new taxonomy. There's no built-in function for this, or at least none that I know of (but I may be wrong).

Anyway, this simple function requires 2 parameters: 1) the term name, 2) the taxonomy name. It returns a boolean. Just paste it in your functions.php file and you're good to go.

Oh, I almost forgot: term names are case-sensitive, so type carefully.

You need to login to post a comment.