0

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!

0

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 );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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