Support » Themes and Templates » How to list next 10 posts combined with pagination

  • Resolved flashlight

    (@flashlight)


    I’m using the following code to create a “next page” link that includes titles of the next ten posts. The idea here being that users may be intrigued to click through if they can see the next ten titles.

    <div id="nextPageLink"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <ul>
    	<?php query_posts('showposts=10&offset=10'); ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<li><a href="<?php echo get_permalink() ?>"><?php the_title(); ?></a></li>
    	<?php endwhile; ?>
    </ul>

    This works well, until the user goes to the next page, in which case they’re presented with the same 10 titles. Is there a way to have the offset increase by 10 depending on which page the user is on?

    Or to put it another way, how can I grab the page number, stick it in a variable, and multiply the offest by that variable?

    Thanks!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • http://wordpress.org/support/topic/57912#post-312858

    You can use that method, or grab the paged value as noted to multiply your offset.

    Thread Starter flashlight

    (@flashlight)

    Thanks Kafkaesqui. I’ve tried the method you describe, as well as multiplying the offset, but am not having success.

    Here’s the code I’m using

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$offset = ($paged * 10);
     	query_posts('showposts=10&offset=$offset'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php echo get_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>

    The offset is not working – I only get the first 10 posts, regardless of which page I’m on. If I echo $offset, I do get the proper value (ie 20 on page 2, 30 on page 3, etc), but that value is not being applied in the query_posts tag.

    Any help is most appreciated.

    Because you use single quotes to enclose your arguments for query_posts(), PHP is handling $offset as a literal string (‘$offset’ not ’20’). Instead, enclose them in double-quotes:

    query_posts("showposts=10&offset=$offset");

    Or concantenate the var to the string using dot syntax:

    query_posts('showposts=10&offset=' . $offset);

    Thread Starter flashlight

    (@flashlight)

    Brilliant, thanks! For the sake of anyone else encountering this, the final code looks like this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged * 10);
    query_posts("offset=$offset"); ?>
    <?php while (have_posts()) : the_post(); ?>

    Just what I was looking for. Thanks flashlight! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to list next 10 posts combined with pagination’ is closed to new replies.