0

I have a category structure like this...

- Shirts
   - Small
         - Red
         - blue
         - green
   - Medium
   - Large
- Jackets
- Hats

...where the ID of 'Shirts' is 1. When I do this...

<ul>
<?php 
query_posts('cat=1&showposts=10&order=ASC'); 
if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    </li>
<?php endwhile; else: ?>
<?php _e('Nothing Here!'); ?>
<?php endif; ?>
</ul>

... instead of showing just the children of Shirts, it is also showing the grandchildren. To illustrate, the output on the screen is showing Small, red, blue, green Medium and Large ,instead of just Small, Medium and Large.

How can I exclude the grandchildren?

Thanks in advance.

Miker
  • 194
  • 2
  • 5
  • 17

2 Answers2

0

Can you try to use category__not_in parameter? For example

query_posts(array('cat' => 1, 'showposts' => 10, 'order' => 'ASC', 'category__not_in' => array(grandchildren_ids)));
  • Thanks but that wont work. Grandchildren categories may be added at any time by the user, an obviously it isn't practical for me to manually exclude them each time new ones are added. – Miker Jul 04 '12 at 00:22
  • I think you can query the database for grandchildren categories, there's no need to hardcode them. – Vladimir Kadalashvili Jul 04 '12 at 07:10
0

Add depth to your query:

<?php query_posts('cat=1&showposts=10&order=ASC&depth=1'); ?>
anuragbh
  • 603
  • 1
  • 5
  • 11