0

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')); ?>
    </a>

    <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(); ?> --> 

1 Answer 1

1

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 ) {
    $all_posts=get_post('category='.$category->term_id);
    foreach( $myposts as $post ){
      setup_postdata($post)
        echo'<li>';'
        echo'<a href="'.the_permalink().'">'.the_title().'</a>';'
        echo'</li>';'
    }
}

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
if(have_post()){
    while(have_posts()){
        the_post();
        //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) )
                continue;
        }
        the_title();
    }
}
2
  • 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. Commented May 17, 2013 at 16:16
  • hey naveen, i edit my answer check third step Commented May 17, 2013 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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