WordPress: Limit numer of posts per page + exclude category

On my home page I have restricted the number of recent posts displayed using query_posts(‘posts_per_page=2’)

I would like to also exclude a category but can’t figure out how to integrate the two modifications to the loop.

Read More

Here’s my original code:

<?php query_posts('posts_per_page=2'); if (have_posts()) : while (have_posts()) : the_post();?>

   <div class="date">Posted <?php the_time('F jS, Y') ?></div>
   <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
   <?php the_content('Continue Reading →'); ?>

<?php endwhile; endif; wp_reset_query();?>

I’ve tried following a few different tutorials but can’t get them to work. I’m sure this is my own lack of understanding, so any clear instructions or modifications to the code above will be greatly appreciated.

Thanks in advance!

Related posts

Leave a Reply

2 comments

  1. Try the cat=-# syntax in your query:

    query_posts('posts_per_page=2&cat=-1');
    

    UPDATED to show how to manually handle it in your loop:

    <?php 
      query_posts('order=ASC'); 
      $count = 0;
      while (have_posts()) {
        the_post();
        $categories = get_the_category();
        if (!in_category('1')) { // category to skip
           $count++;
    ?>
       <div class="date">Posted <?php the_time('F jS, Y') ?></div>
       <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
       <?php the_content('Continue Reading →'); ?>
    
    <?php
           if ($count == 2) break;
         }
      }
      wp_reset_query();
    ?>
    
  2. Please try this:

    <?php
    $specified_cat = new wp_query( 'cat=3&posts_per_page=10' );
    while($specified_cat->have_posts()) : $specified_cat->the_post();
    ?>
    <ul>
    <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    <ul><li><?php the_content(); ?></li>
    </ul>
    </li>
    </ul>
    <?php endwhile; ?>