• On the main page, I want to skip a certain category so they don’t show.

    This code works great from The Loop page in the codex:

    <?php if (in_category('10') && is_home() ) continue; ?>

    The problem is, let’s say I have two recent posts in category 10. I have my blog set to display five posts per page. If I skip these category 10 posts though, only three posts will show.

    How can I skip category 10 posts while keeping the counter loop the same? Or decrement it with something like:

    <?php
       if (in_category('10') && is_home() )
       {
          WP_Query counter--;  //<-- Will this work?
          continue;
       }
    ?>

    But all this will do is make an infinite loop on the category 10 post to skip and never move past it. Or how about I count the number of category 10 posts in the last five. If it’s greater than 0, then temporarily adjust the posts_per_page to 5+(number of cat 10 posts). Then check the 5+(num_cat_10) for yet more cat_10 posts on another loop iteration. If the result is zero, then I finally have the number of posts_per_page.

    The only problem I have with this solution is it’s pretty hacky and I don’t know the function calls to make.

    Related: If I were to temporarily modify my posts_per_page number, let’s say to 7, The ‘Recent Posts’ Widget would display all 7 recent posts, including cat10. What is the code for the Recent Posts? I want to roll my own, but I couldn’t find any base code to modify.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter steneub

    (@steneub)

    Still messing around… I decided to experiment with $post_count, but this didn’t seem to have any effect.

    I placed it as the WP_Query_counter in my previous “example” as both ++ and — and neither had an effect. No infinte loops, so that’s good I guess!

    have you found a solution?

    Any progress on this? I have this exact same issue and it is really becoming a problem.

    I fixed it by using the following code:

    <?php if (have_posts()) : ?>
      <?php query_posts('cat=-xx'); ?>
      <?php while (have_posts()) : the_post(); ?>

    By replacing -xx with the category you DO NOT want included. This will skip it from being included and then you can delete the:

    <?php if (in_category('10') && is_home() ) continue; ?>

    Which will leave your “WP_Query_counter” intact. ??

    How to make this work with the following code:

    <ul>
             <?php if (have_posts()) : ?>
             <?php query_posts('showposts=5'); ?>
             <?php while (have_posts()) : the_post(); ?>
             <?php if (in_category('32')) continue; ?>
           		 <li class="post" id="post-<?php the_ID(); ?>">
                	 <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                 </li>
    
            <?php endwhile; ?>
            </ul>
            <?php endif; ?>

    Here is the code i’m using now and works perfect

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
            <?php static $count = 0;
            if ($count == "5") { break; }
            else { ?>
    
            <?php if ( in_category('32') && !is_single() ) continue; ?>
            <?php if ( in_category('33') && !is_single() ) continue; ?>
    
            <li class="post" id="post-<?php the_ID(); ?>">
                	 <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                 </li>   
    
            <?php $count++; } ?>
            <?php endwhile; ?>
            <?php endif; ?>

    Where ‘5’ is the number of posts that you want to be shown

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Skip a category on the main page, but keep the posts per page constant’ is closed to new replies.