Support » Themes and Templates » list 3 posts with offset and pagination

  • Hi

    I’m trying to code a second loop for index.php template that lists 3 posts with offset=1 and pagination, 5 posts per page. The first loop is showing the latest post.

    I’m following this approach I found here; http://weblogtoolscollection.com/archives/2008/06/19/how-to-offsets-and-paging/

    First, I created this filter in functions.php

    function my_post_limit($limit) {
    	global $paged, $myOffset;
    	if (empty($paged)) {
    			$paged = 1;
    	}
    	$postperpage = intval(get_option('posts_per_page'));
    	$pgstrt = ((intval($paged) -1) * $postperpage) + $myOffset . ', ';
    	$limit = 'LIMIT '.$pgstrt.$postperpage;
    	return $limit;
    } //end function my_post_limit

    Next, I go to index.php and call the filter

    <?php add_filter('post_limits', 'my_post_limit'); ?>

    Then, I start the loop

    <?php
    global $myOffset;
    $myOffset = 1;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('offset='.$myOffset.'&showposts=3'.'&paged='.$paged);
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    Finally, I remove the filter after navigation links

    <div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
    </div>
    
    <?php $wp_query = null; $wp_query = $temp;?>
    <?php remove_filter('post_limits', 'my_post_limit'); ?>

    The offset works great but &showposts=3 doesn’t work, this loop still shows 5 posts that is my wordpress installation default.

    Is there anyway that I can make showposts=3 work in this loop?

  • The topic ‘list 3 posts with offset and pagination’ is closed to new replies.