Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the followind code:

<?php global $post;
            $cat_ID=array();
            $categories = get_the_category(); //get all categories for this post
                foreach($categories as $category) {
                    array_push($cat_ID,$category->cat_ID);
                }
            $args = array(
                'orderby' => 'date',
                'order' => 'DESC',
                'post_type' => 'post',
                'numberposts' => 10,
                'post__not_in' => array($post->ID),
                'category__in' => $cat_ID
            );
            $cat_posts = get_posts($args);
                if ($cat_posts) {

                    echo 'SOME HTML';

                    foreach ($cat_posts as $cat_post) { ?>

It currently lists all post which contain the same category as the current post you are on. What I would like it to do is to exclude the parent category.

The a example category list / example would look like this:

Work
- Animation
- Film
- Misc

When on a post eith the cat Animation all the other posts with the same cat show up (which is good ) but so does the main parent 'Work'. How do I exclude it?

THANK YOU!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Top level categories will have value 0 for the parent, so check that before adding to the array:

foreach( $categories as $category ) {
    if( 0 != $category->parent ) {
        array_push( $cat_ID, $category->cat_ID );
    }
}
share|improve this answer
    
Excellent, thank you! –  greydog Jun 22 '13 at 16:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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