• Resolved markdw

    (@markdw)


    I have a site were I am using the following code to pull in posts from a certain category and display them on the home page.

    <?php $recent = new WP_Query("cat=9&showposts=3"); while($recent->have_posts()) : $recent->the_post();?>
    <?php the_content(__('Read the story »'));?>
    <?php endwhile; ?>

    However this category (ID = 9) has a child category, but I do not want the post from this child category to be displayed as they are displayed elsewhere on the homepage.

    How can I modify this code (or use other code) to change this query so that it doesn’t display posts from a child category. Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter markdw

    (@markdw)

    Fixed with the following:

    <?php $recent = new WP_Query("cat=9,-26&showposts=3"); while($recent->have_posts()) : $recent->the_post();?>
    <?php the_content(__('Read the story »'));?>
    <?php endwhile; ?>

    The negative number (-26) excludes category number 26.

    Wow thanks blepoxp!
    I have been looking to exclude child categories from the parent category for days, finally came across this post with your tip and “voila”, works like charm!
    And the good thing is that it only works on the main category template, so you can still use category-x.php for other views if necessary.
    I’m a happy surfer now 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Exclude Posts from Child Categories’ is closed to new replies.