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>
    if ( is_single() ) {
    $cats = wp_get_post_categories($post->ID);
    if ($cats) {
    $first_cat = $cats[0];
      'cat' => $first_cat, //cat__not_in wouldn't work
      'post__not_in' => array($post->ID),
    $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="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title() ?></a></li>
    } //if ($my_query)
  } //if ($cats)
  wp_reset_query();  // Restore global post data stomped by the_post().
} //if (is_single())

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;

Comments inline:

     global $wp_query;
    //* first let's get current post parent category ID

    $ID = $wp_query->posts[0]->ID; //* get current post ID
    $postcat = get_the_category($ID); //* based on post ID, get current post categories IDs -> WP function returns array of stdClass Objects, one stdClass Object for each category; if there is child - parent relationship, first it will return Objects for children categories and than for parents -> add "print_r ($postcat);" without quotation marks in next row to see content of array
    $cat = $postcat[0]->cat_ID; //* get first category ID from first Object in $postcat array (if post has children categories, it will get first child ID, else it will get first category ID)
    $thiscat = get_category ($cat); //* get array of category objects for $cat (current) category -> add "print_r ($thiscat);" without quotation marks in next row to see content of array
    $parent = $thiscat->category_parent; //* and extract parent category ID; if category has no parent category (and parent category has no parent), ID = 0

    //* now we have current category parent ID No, or if category has no children ID = 0
    //* let's check if we heave categroy with children categories or not

    if ($parent == 0) //* if parent ID = 0, it is category without parent category    
        {} //* do nothing 

    else { //* else, if it has children categories

        $subcategories = get_categories('child_of='.$parent); //* get array of children categories Objects and

        $items=''; //* create new ArrayObject and

            foreach($subcategories as $subcat) { //* foreach child category in array
                if($thiscat->term_id == $subcat->term_id) //* check if current category ID is same as child category ID
                $current = ' current-cat'; //* if true, add current-cat to li class
                else $current = ''; //* if not, add nothing

                $items .= '
                <li class="cat-item cat-item-'.$subcat->term_id.$current.'"> <!-- if is child current, add here .current-cat //-->
                    <a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.' ('.$subcat->count.' posts)</a> <!-- link to -> category link, title -> category description, show -> category name and No of posts //-->

        echo "<ul>$items</ul>";
  • Just some sidenotes: You'll have to declare global $wp_query; first. $ID would be global $post; $post->ID;. The get_category() call will take the current posts ID anyway. if ( $parent == 0 ) should (for readabilities sake) be: if ( ! $parent ) or if ( $parent === false).
    – kaiser
    Commented May 23, 2011 at 21:37
  • @will and @kaiser - thank you both so much for your help!
    – Alli
    Commented May 23, 2011 at 22:21
  • @kaiser - can you help me understand how to declare global $wp_query; I am a beginner and right now I can't get this code to work in my sidebar (although I sense that it will do what I need it to eventually!!)
    – Alli
    Commented May 23, 2011 at 22:22
  • Just write global $wp_query inside the function (on top), so you access the global object in the function.
    – kaiser
    Commented May 23, 2011 at 23:21
  • @kaiser So the way this is working now, it's printing a list of links to category pages for the child categories when there is a parent category associated with the post. When there are no child categories, I get links for all of the parent categories showing up in the sidebar. What I'm trying to do is to show similar items (posts - not categories) in the sidebar of a post view that are all in the same child category as the post. If there aren't similar items, instead of showing parent categories, I'd like it to display "This project is unique." Is that possible do you think?
    – Alli
    Commented May 24, 2011 at 14:18
$categories = get_categories('child_of='.INSERT_YOUR_CATEGORY_PARENT_HERE.''); 
foreach ($categories as $category) {
    echo $category->name;

Editors note: INSERT_YOUR_CATEGORY_PARENT_HERE has to be replaced, else it would be seen as undefined constant and throws an Error.

  • 1
    Please provide some context for your code; what it does and how it works.
    – shea
    Commented Jul 26, 2013 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.