Skip to content

Instantly share code, notes, and snippets.

@matthewcrist
Created June 5, 2010 22:36
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 matthewcrist/427063 to your computer and use it in GitHub Desktop.
Save matthewcrist/427063 to your computer and use it in GitHub Desktop.
function post_is_in_descendant_category($cats, $_post = null) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category');
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
function is_my_current_cat($id) {
if (is_category($id))
return true;
if (is_category()) {
$descendants = get_term_children( (int) $id, 'category');
if ( $descendants )
foreach($descendants as $cate){
if(is_category($cate ))
return true;
}
}
if (is_single()) {
if (in_category($id) || post_is_in_descendant_category($id)) {
return true;
}
}
return false;
}
A couple helpers to help you figure out if you're within a category in Wordpress. A slight modification of code found here...
http://olykrap.com/post/how-to-check-for-category-descendants/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment