WordPress.org

Ready to get started?Download WordPress

Forums

Show posts except from few subcategories (5 posts)

  1. nims
    Member
    Posted 7 years ago #

    I am showing latest 5 posts in category.php but want to hide (or do not want to show ) posts from few subcategories under particular category.

    Example : If I am in category C that has sub-categories c1, c2, c3, c4 and c5. I want to display latest 5 posts except posts in subcategory cat-3

    I hv tried it but the problem is that the loop runs 5 times and counts the skipped posts as well, though they are not displayed. So if there are 4 latest posts from c3, the category page shows just 1 latest post , since it skips the subcategory c3's 4 latest posts.

    here the code I am using

    <?php if (have_posts()) : ?>
    
    			 <? if (is_category('8') or is_category('2')) { ?>
    				 <?php while (have_posts()) : the_post(); ?>
    				<? if ((! in_category('14')) and (! in_category('10')) and (! in_category('16'))) {?>
    
    				 <div id="category">
    				  <!-- <h2 id="post-<?php the_ID(); ?>"> -->
    				  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php single_cat_title('');?> <?php the_title(); ?>">
    					<?php the_title(); ?></a></h2>
    					<?php the_excerpt(); ?> <?php the_time('F jS, Y') ?> <a href="<?php the_permalink() ?>" rel="bookmark">[ ... Read more ...]</a>
    					<!-- by <?php the_author() ?> -->
    				</div>
    				<? } ?>
    				<?php endwhile; ?>
    			<?}?>
  2. nims
    Member
    Posted 7 years ago #

    Any help ?

  3. cwyounts
    Member
    Posted 6 years ago #

    I'm having the same problem, and just increasing the number of posts shown doesn't work for several reasons. Is there any code that tells the loop not to count the hidden category posts? I can't add any more plugins, I'm interested in code only.

    Thanks!

  4. cwyounts
    Member
    Posted 6 years ago #

    Whoops, I guess I should mentione I'm using version 2.2.2.

  5. cwyounts
    Member
    Posted 6 years ago #

    Aha, found it after a little more searching (only took me a week!) Turns out I needed to exclude the category BEFORE the Loop in index.php:

    <?php
    if (is_home()) {
    query_posts("cat=-7");
    }
    ?>

    Worked like a charm!

Topic Closed

This topic has been closed to new replies.

About this Topic