Support » Themes and Templates » pagination issue due to complex loop

  • Resolved badr



    I’m using a slightly weird loop on my index page so that I can display a list of the last 5 post titles above the content then display the last 10 posts right below that. All of this while excluding one category..
    problem is, now, pagination is no longer working.. I know it’s due to the following code but i’m not sure how to go about fixing it:

    <?php query_posts('cat=-7&showposts=5'); ?>
      <?php while (have_posts()) : the_post(); ?>
      <li class="funky"><a href="<?php the_permalink() ?>" class="funky"><?php the_title(); ?></a></li>
      <?php endwhile;?>
    <!-- begin the articles -->
    <p class="meta2"> The Knowledge </p>
    <?php query_posts('cat=-7&showposts=10'); ?>
       <!-- <?php if (have_posts('cat=-7,-8,-9&showposts=10')) : ?> -->
      <?php while (have_posts()) : the_post(); ?>
      <div class="post" id="post-<?php the_ID(); ?>">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	  <span class="user"><?php the_time('D M d, Y') ?> at <?php the_time('g:i a'); ?></span>
    	  <span class="user2">By <?php the_author() ?> </span>
        <div class="entry">
          <?php the_content('Read More &rarr;'); ?>
    <p class="postmeta">
          <span class="commr"><?php comments_popup_link('No Comments &rarr;', 'Comment (1)', 'Comments (%)'); ?></span>
     &nbsp; &nbsp; | &nbsp; &nbsp; <span class="catr">Channel: <?php the_category(', ') ?></span>
    &nbsp; &nbsp; | &nbsp;  <?php edit_post_link('Edit', '<span class="editr">', '</span>'); ?>
      <?php endwhile; ?>
      <div class="navigation">
        <div class="alignleft">
          <?php next_posts_link('&larr; Previous Entries') ?>
        <div class="alignright">
          <?php previous_posts_link('Next Entries &rarr;') ?>
Viewing 11 replies - 1 through 11 (of 11 total)
  • See:

    Keep in mind the multiple loops both using query_posts() may still cause issues if you need to paginate just one of them (assuming the second). If you need pagination on both… well, you’re on your own. :)

    Thanks. I see how the link will help me solve this.

    it sounds as if you’re suggesting using both loops is a bad thing. Can I impose on you with another question and ask if there is a way to get rid of the first one? All I’m using it for is to display the latest posts… is there a more elegant way?


    it sounds as if you’re suggesting using both loops is a bad thing.

    Not quite. I just wanted to warn that once you start playing with multiple post loops and pagination and whatnot, handling each loop properly can be difficult, based on what they’re expected to do.

    Multiple uses of query_posts() (and sometimes just a single use of it) often lead to unintended consequences — especially in conjunction with the default loop (i.e. The Loop). But with all post loops in your template appearing to be initialized by a query_posts(), you probably won’t hit any big problems.

    thanks Kafkaesqui .. I really appreciate your advice

    so I’ve replaced the second loop with:

    if (is_home()) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    <!-- <?php query_posts('cat=-7&showposts=10'); ?>
        <?php if (have_posts('cat=-7,-8,-9&showposts=10')) : ?> -->
      <?php while (have_posts()) : the_post(); ?>

    but It’s still not working… where did I mess up?

    Hmm. Thought so. First, try modifying your first (latest posts) query to:

    <?php $latest = new WP_Query('cat=-7&showposts=5'); ?>
      <?php $latest->while(have_posts()) : $latest->the_post(); ?>

    This will avoid any conflicts between it and the second one generated through query_posts().

    Second, remove:

    <!-- <?php query_posts('cat=-7&showposts=10'); ?>
        <?php if (have_posts('cat=-7,-8,-9&showposts=10')) : ?> -->

    as well as locate and remove the closing endif; in your template (wherever it is). The use of <!-- ... --> is meant for commenting out HTML. PHP code will still run in it, hence the need to remove it entire, or to use PHP commenting within the <php> tags:

    <?php // query_posts('cat=-7&showposts=10'); ?>
        <?php // if (have_posts('cat=-7,-8,-9&showposts=10')) : ?>
    <?php // endif; ?>

    🙁 nope..

    I did that and now I get:
    Parse error: syntax error, unexpected ':' in /-------/index.php on line 8


    Upload template here:

    and reply back with the link to it.

    thank you so much for the help.. I reverted to the original index now. Here is the link:

    This should do:

    In the end I decided to leave the if/endif alone, seeing you have an else statement outputting a ‘not found’ message.

    thank you yet again…

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘pagination issue due to complex loop’ is closed to new replies.