views:

116

answers:

1

Hi,

I'd like to know how to prevent showing of sub-category posts. My home page lists all posts from three "main categories" (parent category), but unfortunately it's also listing some posts from the sub-categories.

Here's the code that I'm using to get the posts from specific category:

<h2>Category Name</h2>
<ul>
    <?php $category_query = new WP_Query(array('category_name' => 'category1', 'showposts' => 5)); ?>
    <?php while ($profissionais_query->have_posts()) : $profissionais_query->the_post(); ?>
    <li>
        <a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_excerpt(); ?>
    </li>
    <?php endwhile; ?>
</ul>

Does anyone have an idea?

Thank you.

+1  A: 

Try this style of new query; it only shows the one category. It can be used mutliple times in a page or post (with php execution enabled) without conflict:

<?php $my_query = new WP_Query('category_name=mycategory&showposts=5'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php the_excerpt(); ?>
<?php endwhile; ?>
songdogtech
It's good practice to add <?php wp_reset_query();?> to destroy custom queries after they run otherwise they can effect other queries being run on the page. Add it just before the endwhile.
Jared
songdogtech: unfortunately this piece of code didn't work, because it still showing the sub-category posts. Do you have another idea? Thank you.
Carlos Pattrezzi
Carlos; I tested it with subcategories and it does not show subcategories; subcategories have their own category ID and have to be called specifically. Try it in a page template with no other loops or code (other than the standard WP loop) to isolate the conflict.Jared: It doesn't need a wp_reset_query as it is a self-contained query. I use it on several sites, mutliple times on a page/post and none of the query loops conflict.
songdogtech