How to exclude parent category but show child categories?

I figured out how to exclude the current post from displaying in my single-post view sidebar, but now I want to exclude parent categories and display only children. Any ideas how to do this? Much obliged!!

<div id="sidebar" class="grid_4">
<div class="item portfolio"><hr></hr>
    <h4>Similar Items</h4>
<?php
    if ( is_single() ) {
    $cats = wp_get_post_categories($post->ID);
    if ($cats) {
    $first_cat = $cats[0];
    $args=array(
      'cat' => $first_cat, //cat__not_in wouldn't work
      'post__not_in' => array($post->ID),
         'showposts'=>8,
      'caller_get_posts'=>1
);
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>

<?php if (in_category('15')) continue; ?>

<div ID="similarentry"><ul>
            <li ID="similarthmb"><?php get_the_image( array( 'custom_key' => array( 'thumbnail' ), 'default_size' => 'thumbnail', 'width' => '119', 'height' => '75', 'link_to_post' => true ) ); ?></li>
            <li ID="similarttl"><a href="https://wordpress.stackexchange.com/questions/18047/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title() ?></a></li>
        </ul></div>
       <?php
      endwhile;
    } //if ($my_query)
  } //if ($cats)
  wp_reset_query();  // Restore global post data stomped by the_post().
} //if (is_single())
?>
</div></div>

3 Answers
3

I spent almost two months building and debugging Total Widget Control to do exactly this for all of a sites widgets. Bear with me as I strip the code out of Total Widget Control and try to make it usable for you.

    global $wp_query
    $twc_menu_item_object = $wp_query->object_id; //is the object id of the current page.
    $objects['menu-item-object'] = ''; //the object id of the tax to display under

    if ( taxonomy_exists($twc_menu_item_object) && isset($objects['menu-item-object']) )
    {
        foreach ($objects['menu-item-object'] as $menu_id => $menu_type)
        {
            if ( $menu_type != $twc_menu_item_object ) continue;
            if ( !cat_is_ancestor_of( $menu_id, $object_id ) ) continue;
            return true;
        }
    }
     return false;

Leave a Comment