You can use the post_is_in_descendant_category() to look if a post is in a child category of the category you have specified in the function.

So you can use it for example in your <title> in an if statement to change your <title> if the post is in a parent category that you had specified in the function. Example:

<?php if(post_is_in_descendant_category('1') ) : ?>
// Do something
<?php endif; ?>

In this example (‘1’) is a parent category.

This function is not standard in WordPress so you need to place the following code in your functions.php.

 * Tests if any of a post's assigned categories are descendants of target 
 * @param int|array $cats The target categories. Integer ID or array of integer 
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
 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;

3 thoughts on “Post_is_in_descendant_category

  1. Pingback: Определение родительской категории для дочерних в шаблоне wordpress – Post_is_in_descendant_category | Web First Step

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s