<?php
/**
* Tests if any of a post's assigned categories are descendants of target categories
*
* @param int|array $cats The target categories. Integer ID or array of integer IDs
* @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 http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_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;
}
?>
List order by category (category name and descendant id)
<?php if ( in_category('News') || post_is_in_descendant_category(69) || in_category('Featured')) { ?>
<?php } else { ?>
<?php $posts = query_posts( $query_string . '&orderby=title&order=asc' ); ?>
<?php } ?>
List sub categories (category name and descendant id)
<?php if ( in_category('News') || post_is_in_descendant_category(19)) { ?>
<ul id="subcats">
<?php
if (is_category()) {$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
wp_list_categories('hide_empty=0&title_li=&depth=1&child_of=' . $this_category->cat_ID);
}
else {
$parent = $this_category->category_parent;
wp_list_categories('hide_empty=0&title_li=&child_of=' . $parent);
}
}
?>
</ul>
<?php } else { ?>
<?php } ?>
Display per category (category name)
<?php if (is_category('test')) { ?>
<p>display list</p>
<?php } else { ?>
<?php } ?>
Display per category with if else
<?php if (is_category('test 1')) { ?>
<p>Test 1</p>
<?php } elseif (is_category('test 2')) { ?>
<p>Test 2</p>
<?php } elseif (is_category('test 3')) { ?>
<p>Test 3</p>
<?php } else { ?>
<p>Test nothing</p>
<?php } ?>