WordPress: in subcatory?


/ Published in: PHP

Usually use in a template with `in_category()`, like `if ( in_category( 3 ) || in_subcategory( 3 ) ) { ...`. Tested in WP 2.8.4.


Copy this code and paste it in your HTML
  1. function in_subcategory( $cat_id ) {
  2. $subcats = get_term_children( $cat_id, 'category' );
  3. if ( $subcats && !is_wp_error( $subcats ) ) {
  4. foreach( $subcats as $child_category ) {
  5. if( in_category($child_category) ) {
  6. return true;
  7. }
  8. }
  9. }
  10. return false;
  11. }

URL: http://codex.wordpress.org/Function_Reference/get_term_children

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.