I am having an issue that I think falls into this thread, though it is using condition descendants outside of widgets.
I have a parent category called ‘Sciences’ with ID=33; and a number of sub-categories within it. Here is the end goal: For every post within a child category of ‘Sciences’, I want to display the child category’s description below the post.
So if I have a post in category ‘Biology’ (ID=11) which is a child of the category ‘Sciences’, I want the Biology cat description to display. And if another post is in category ‘Geology’ (ID=12), also a child of ‘Sciences’, I want the Geology cat description to display.
I am just having a bit of trouble understanding how to use post_is_in_descendant_category.
Right now I have this code in my single.php file:
<div id=postinfo>
<?php if ( in_category( 'Sciences' ) || post_is_in_descendant_category( 33 ) ) {
}
?>
</div>
and I also have this code in my functions.php file:
<?php if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<ul class="category-bg widget %2$s">',
'after_widget' => '</ul>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
?>
<?php
/**
* Tests if any of a post's assigned categories are descendants of target categories
*
* @param mixed $cats The target categories. Integer ID or array of integer IDs
* @param mixed $_post The post
* @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() Gets descendants of target category
* @uses in_category() Tests against descendant categories
* @version 2.7
*/
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;
}
?>
As you can see, most of this is just copied from the Codex (I haven’t gotten to the description part yet) and Im a bit hung up here.
Any advice in how to yield the kind of output I’m looking for? Any help is greatly appreciated!
-Thanks
-ac