matt
matt

Reputation: 1

How can I exclude child post from its parent category in Wordpress?

I've created category A and then categories B and C which are both child of A. When I visit category A all posts belonging to that category are listed, but it also shows all posts that belongs to category B and C even that those post are not assigned to category A. Is it possible to show only posts under category A and exclude all posts from it childs B and C? I've seen lot of answers but most of then are old and I'm not sure if they work with current version. Also, I'm just starting, and even that I've seen lots of examples of how to overcome this issue it's hard for me to figure where should I make the changes.
Can you guide me in where and what do I need to change in order to achieve this? Thank in advance for your help.

UPDATE

I'm new and can't answer my own questions yet. But I wanted to share what I've found to help anyone else with the same issue. Just place it this code before the loop.

  <?php 
      if ( is_category() ) {
      $cat = get_query_var('cat');
      query_posts(array('category__in' => array($cat)));
      }?>

Thanks to all for your time and help.

Upvotes: 0

Views: 2833

Answers (2)

Nazim
Nazim

Reputation: 564

In the category.php page just put this code just before the loop

<?php

if ( have_post() ) {
        if (is_category()){
         $category = get_category(get_query_var('cat'));
         $child_cats = array();
     $child_cats = get_term_children($category, 'category');
     query_posts(array('category__not_in' => $child_cats);
         }
   }


 while (have_posts()) : the_post(); $postcount++; ?>

Upvotes: 2

Gaurav Parashar
Gaurav Parashar

Reputation: 126

When you fetching you post data, you have to specify the category id of you post. I think this may help you .

Upvotes: 0

Related Questions