WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wanted to list posts which are directly under a category and not inside the "sub category". In other words, I want to hide all the posts which are from "Sub Category" in the Parent category view.

My looping code is below.

<?php rewind_posts(); if (have_posts()) : while ( have_posts() ) : the_post(); global $post; ?>
            <?php get_template_part('includes/loop'); ?>
        <?php endwhile; wp_reset_query(); ?>    

<!-- loop.php -->

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <a href="<?php the_permalink(); ?>" rel="bookmark">
        <?php the_post_thumbnail('home-entry-thumb', array('class' => 'entry-thumb')); ?>

    <h2 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

    <div class="entry-excerpt">
         <?php the_excerpt(); ?>  
        <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'themejunkie' ), 'after' => '</div>' ) ); ?>          
    </div><!-- .entry-excerpt -->

    <div class="clear"></div>

</div><!-- #post-<?php the_ID(); ?> --> 
share|improve this question

to solve this you can create a page template, on this page template

1.get the top level categories only, set parent value to zero

$args = array(
  'orderby' => 'name',
  'parent' => 0
$categories = get_categories( $args );

2.get posts in all parent categories

foreach ( $categories as $category ) {
    foreach( $myposts as $post ){
        echo'<a href="'.the_permalink().'">'.the_title().'</a>';'

3.if you not want to create page template, then

in category.php(modify it as your design requirement)

 // Get children categories of current cat if they exist
if ( $excludes = get_categories( "child_of=" . $wp_query->get( 'cat' ) ) ) {
    // For each child, add just the ID to an array
    foreach ( $excludes as $key => $value ) {
        $exs[] = $value->term_taxonomy_id;

// In Loop
        //get all category of current post
        $categories = get_the_category();
        foreach($categories as $category) {
            //check if any category is child category of current category 
            if( in_array($category,$exs) )
share|improve this answer
Correct me if I'm wrong, but a page is not what I'm looking for.. I have 150 categories, which have 3-4 sub categories each. So I have around 600 categories, creating pages for each wont be a solution. I believe there is a "category__in" function which can be used in the loop to only list post from the current category and exclude it's sub categories. – Naveen Narale May 17 '13 at 16:16
hey naveen, i edit my answer check third step – Ravinder Kumar May 17 '13 at 16:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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