See the comments in code for explaination
// all the categories for the post
$categories = get_the_category();
// extracts ids form post categories
$categories_ids = wp_list_pluck( $categories, 'term_id' );
// all the categories id that are descendant of category 12
$children = get_terms('category', array('child_of'=>12, 'fields'=>'ids') );
// all the categories id for the post that are descendant of category 12
$neighborhood = array_intersect($categories_ids, $children);
// show the div only if we have neighborhood
if ( ! empty($neighborhood) ) {
echo '<div class="neighborhood">What's Happening in:';
foreach ( $categories as $cat ) {
if ( ! in_array($cat->term_id, $neighborhood) ) continue;
echo '<a href="'. get_category_link($cat->term_id) . '">' . $cat->name . '</a>';
}
echo '</div>';
}
More on: