• I’m a little bit stumped as to how I should modify the loop so that my home page displays an offset of posts.

    I have a featured slider that I want to contain the fist ‘x’ number of posts and would like the remainder to show up below those. At the moment, the slider is showing 3 posts and then following up with the same 3 posts below it.

    I’ve tried adding a wp_query with an offset to the last section of the loop but it causes the page to go into an endless loop.

    Any help appreciated. Thanks.

Viewing 1 replies (of 1 total)
  • From the Codex, here’s some code that is similar to what you want:

    <?php $my_query = new WP_Query('posts_per_page=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
        <!-- Do stuff... -->
      <?php endwhile; ?>
        <!-- Do other stuff... -->
      <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; ?>
       <!-- Do stuff... -->
      <?php endwhile; endif; ?>

    Basically what you are doing is running a loop and storing the ID of the post in a variable. Then you run a second loop and compare the posts to said variable, and if it matches, you don’t show that post. The problem is the above code only works if you are showing one post, and then the rest. To show the first three posts, and then the rest, you’d have to modify it like below (hasn’t been tested but should be close to what you need):

    <?php
      $i = 0;
      $my_query = new WP_Query('posts_per_page=3');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate[$i] = $post->ID;?>
        <!-- Do stuff... -->
      <?php $i++; endwhile; ?>
        <!-- Do other stuff... -->
      <?php
      $i = 0;
      if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate[$i] ) continue; ?>
       <!-- Do stuff... -->
      <?php $i++; endwhile; endif; ?>

    Basically the difference is the variable is now an array, and so it should store the ID’s of all three posts.

Viewing 1 replies (of 1 total)

The topic ‘Twenty Ten – Offset Home Page Posts’ is closed to new replies.