Guest User

has_category tribe events

a guest
Apr 22nd, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.44 KB | None | 0 0
  1. /**
  2. * Conditional function to check if post belongs to term in a custom taxonomy.
  3. *
  4. * @param    tax        string                taxonomy to which the term belons
  5. * @param    term    int|string|array    attributes of shortcode
  6. * @param    _post    int                    post id to be checked
  7. * @return             BOOL                True if term is matched, false otherwise
  8. */
  9. function pa_in_taxonomy($tax, $term, $_post = NULL) {
  10.     // if neither tax nor term are specified, return false
  11.     if ( !$tax || !$term ) { return FALSE; }
  12.     // if post parameter is given, get it, otherwise use $GLOBALS to get post
  13.     if ( $_post ) {
  14.         $_post = get_post( $_post );
  15.     } else {
  16.         $_post =& $GLOBALS['post'];
  17.     }
  18.     // if no post return false
  19.     if ( !$_post ) { return FALSE; }
  20.     // check whether post matches term belongin to tax
  21.     $return = is_object_in_term( $_post->ID, $tax, $term );
  22.     // if error returned, then return false
  23.     if ( is_wp_error( $return ) ) { return FALSE; }
  24.     return $return;
  25. }
  26.  
  27. // get tribe categories in an array if you need them all (optional)
  28.             $terms = get_terms("tribe_events_cat");
  29.             $count = count($terms);
  30.             $tribe_cats_me = array();
  31.             if ( $count > 0 ){
  32.                 foreach ( $terms as $term ) {
  33.                     $tribe_cats_me[] =  $term->slug;
  34.                 }
  35.             }
  36.            
  37. // check if this post has a category (the second argument) ; here I've used an array of categories         
  38. if (pa_in_taxonomy('tribe_events_cat', $tribe_cats_me )) {
  39.     // do true stuff here
  40. }
Advertisement
Add Comment
Please, Sign In to add comment