0

Ok, so I have added the following pagination tags:

<?php next_posts_link( 'Older posts' ); ?>
<?php previous_posts_link( 'Newer posts' ); ?>

I can see in the address bar that the querystring is changing correctly

e.g. localhost:8888/wordpress/?paged=3

However, the page results are staying the same (showing the original first 10 results).

What have I got wrong here?

EDIT:

Here is the code (without any of the HTML):

<!--Latest Post-->
<?php query_posts('posts_per_page=1'); while(have_posts()) : the_post(); ?>
    <?php echo get_avatar( get_the_author_meta( 'ID' ), 125 ); ?>
    <?php the_title(); ?>
    <?php the_time('F jS, Y'); ?>
    <?php the_author(); ?>
    <?php the_excerpt(); ?>
<?php endwhile; wp_reset_query(); ?>    

<!-- Next set of posts to display on page -->
<?php query_posts('posts_per_page=10&offset=1'); while(have_posts()) : the_post(); ?>       
    <?php echo get_avatar( get_the_author_meta( 'ID' ), 125 ); ?>
    <?php the_title(); ?>
    <?php the_time('F jS, Y'); ?>
    <?php the_author(); ?>
    <?php the_excerpt(); ?>
<?php endwhile; wp_reset_query(); ?>

<!--Pagination-->
<?php next_posts_link( 'Older posts' ); ?>
<?php previous_posts_link( 'Newer posts' ); ?>

Hopefully that all looks ok. The page displays nicely, its just the pagination that is not behaving... yet.

7
  • Did you try the troubleshooting steps here: http://codex.wordpress.org/Pagination Commented Aug 19, 2014 at 11:58
  • Hey, had a look at that but it didn't help me :( I am taking the advise from that page and asking on a forum ;) - To further explain, I have my own theme, I have a loop which pulls the first result into a "hero" block and then I have another loop with an offset=1 to start the next set of posts... would it help if I posted my code?
    – steakpi
    Commented Aug 19, 2014 at 12:07
  • Definitely post your code. And on which template are you doing it. Custom queries and specially offset stuffs around with pagination Commented Aug 19, 2014 at 12:12
  • I've done something similar in this post. And PLEASE don't use query_posts, ever Commented Aug 19, 2014 at 15:12
  • Hey Pieter, I tried running through what you had done in that post you had done before. It mostly worked up until I added the code into the functions.php. It was the only function in there.. and I removed all whitespace to prevent "white screen of death" but no such luck. Tomas's solution below does work, but now im concerned about the use of "query_posts".. not sure why they are bad (lack of php knowledge)... but I would rather go down the "preferred" route if it will reduce issues in the future.
    – steakpi
    Commented Aug 19, 2014 at 16:07

1 Answer 1

1

You should use WP_Query here, also, for pagination, you need to use the paged parameter, because Wordpress need it to calculate the offset.

I guess you could replace both queries with WP_Query, and to avoid showing the first post twice, save the ID in a variable and pass it to the post__not_in parameter in the second query.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'posts_per_page' => 10,
      'paged' => $paged,
      'post__not_in' => $first_post

    );

<?php query_posts($args); while(have_posts()) : the_post(); ?>       
    <?php echo get_avatar( get_the_author_meta( 'ID' ), 125 ); ?>
    <?php the_title(); ?>
    <?php the_time('F jS, Y'); ?>
    <?php the_author(); ?>
    <?php the_excerpt(); ?>
<?php endwhile; wp_reset_query(); ?>

And $first_post is an array with the id of the first post. You can set it like this:

$first_post = array(get_the_ID());

This code should be in the first loop.

This is using your code, which isn't recommended, but it should work anyway.

4
  • Hi Tomas, the page displays correctly already. When I click on the pagination links, I do get the "paged" querystring in the address bar. The problem is that the page does not go to the next set of results, it keeps the original ones. Im still pretty new at this.. will your suggestion help? If so would you be able to show what you mean in the code? thanks in advance :) -- Also.. this is all on the index.php. Do I need another PHP page created?
    – steakpi
    Commented Aug 19, 2014 at 15:04
  • @steakpi, try that.
    – Tomás Cot
    Commented Aug 19, 2014 at 15:24
  • @steakpi, if you keep using Wordpress, try to revisit that code, and use WP_Query instead.
    – Tomás Cot
    Commented Aug 19, 2014 at 16:15
  • Will do!.. I think I can follow part of Pieter Goosen's guide for that. Hopefully I will get it right. :)
    – steakpi
    Commented Aug 19, 2014 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.