Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?> --> 
share|improve this question
add comment

1 Answer 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();
    }
}
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 –  Ravs May 17 '13 at 16:51
add comment

Your Answer

 
discard

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.