特定の親カテゴリーに属する子・孫カテゴリーに反映させる条件分岐

2015/10/05
functions.phpにコピー
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
	function post_is_in_descendant_category( $cats, $_post = null ) {
		foreach ( (array) $cats as $cat ) {
			$descendants = get_term_children( (int) $cat, 'category' );
			if ( $descendants && in_category( $descendants, $_post ) )
				return true;
		}
		return false;
	}
}

テンプレート内
<?php if(post_is_in_descendant_category(3)): ?>
<?php else: ?>
<?php endif; ?>