Skip to content

Instantly share code, notes, and snippets.

@ninnypants
Created October 27, 2011 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninnypants/1320182 to your computer and use it in GitHub Desktop.
Save ninnypants/1320182 to your computer and use it in GitHub Desktop.
Test if a post is in a specific taxonomy
<?php
/*
** $taxonomy = Taxonomies to retrieve terms from
** $tax = Term slug or id to test against
*/
function in_tax($taxonomy, $tax){
global $post;
$taxes = array();
if(is_int($tax)){
$terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));
}else{
$terms = wp_get_object_terms($post->ID, $taxonomy);
foreach($terms as $term){
$taxes[] = $term->slug;
}
$terms = $taxes;
}
return in_array($tax, $terms);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment