post-page

How to: Offsets and Paging

31
responses
by
 
on
June 19th, 2008
in
HOW-TO, WordPress FAQs, WordPress Hack
heading
heading
heading
31
Responses

 

Comments

  1. Justin Tadlock (51 comments.) says:

    Very cool, Ronald. This is something I’ve been meaning to figure out. I don’t use offset much myself but have a few theme users that do. I’m sure this will help out.

    You just saved me loads of time.

  2. Jeff says:

    An offset? What’s an offset?

  3. John Kolbert (23 comments.) says:

    Great write up Ronald. I’m using it on my site right now! Thanks!

  4. Ian Stewart (28 comments.) says:

    I swear, Ronald comes up with the best tutorials.

  5. Ronald Huereca (66 comments.) says:

    Thanks y’all :)

  6. John Kolbert (23 comments.) says:

    @Jeff: An offest means how many posts it should skip before displaying the rest of them. Setting offset equal to one means it skips your latest post and starts and the next one.

  7. Martin Welch (5 comments.) says:

    Thanks for sharing the mini tutorial, it’s big help especially for those who want to use offset and paging. :)

  8. Dresah (2 comments.) says:

    Hi,
    is there a link which we can see an example?

  9. Ronald Huereca (66 comments.) says:

    @Dresah,

    I’m not sure what kind of example you’d want, since all it would be is a screenshot of a content block with the next/previous links.

    You can kind of see it at my site raproject under the ‘Recent Articles’ heading, although I don’t use offsets.

  10. Dresah (2 comments.) says:

    @Ronald Huereca,

    Thanks, this is enough for me :)

  11. Roni (9 comments.) says:

    I will try it, since I got confused to put page navigation on my site, I’ve used wp-page navi but seem not work well on my themes. Maybe you can give me a direction, then visit my site

    Best regard

  12. Morsels of Memory (2 comments.) says:

    First of all, thanks for sharing the knowledge. I am trying to customize my WP so that index.php displays 1 post only and the archive pages display 5 posts per page. I tried the codes you provided both in this post and in the post on Paging and Custom WordPress Loops. Despite tricking the WP into using $wp_query in the loop like Aaron suggested, the maximum number of pages displayed is still controlled by the “posts per page” option set in Settings < Reading, which I set to 5. I currently have 10 posts. Therefore, on my index.php, the pagination navigation only goes to page 2, and returns a 404 error beyond page 2. Do you know how to tweak the codes so that on index.php, the page navigation will return all 10 pages (1 post per page), while on the archives pages it will return 2 pages (5 posts per page)? I suspect this involves tweaking the $max_num_pages variable used in the navigation functions in the links_template.php file. Right now the default sets the same $max_num_pages for all !is_single() files. I tried to set a conditional $max_num_pages for is_(home), so that the maximum number pages displayed will be equal to the total number of posts, not the value (total posts divided by posts per page) calculated by WP based on the posts per page option set in Settings. I haven’t been able to come up with the correct expression, if you have any ideas to share, it would be great for myself and many others. I’ve seen quite a few unresolved threads on this pagination issue in the WP Forums. Thanks again.

  13. Morsels of Memory (2 comments.) says:

    Hi, Sorry, there was a typo in my comment. Toward the bottom of my comment, I meant the conditional statement is_home(), not is_(home). Just wanted to clarify that. Thanks.

  14. celita says:

    Dear Roanld, thanks A LOT for your excellent solution and tutorial. You had helped me with a problem that had annoyed my husband for a long time. T-H-A-N-K-S !!

  15. Ian Stewart (28 comments.) says:

    I finally had reason to use this in a project. Thanks again for this tutorial.

  16. Jose says:

    Superb code!

    However, showposts=5 doesn’t work if changed to a different value

  17. John Kolbert (23 comments.) says:

    To change the number of posts shown you have to set the $postperpage variable in the “my_post_limit” function to whatever number you want. For some reason the showposts doesn’t work.

  18. Jose says:

    Thanks for your reply John :)

    $postperpage=3 will work but when you remove the filter, how can you set $postperpage to wordpress default again?

  19. Greg (1 comments.) says:

    Something to note: the $wp_query->max_num_pages variable will occasionally be incorrect when using this code. For example, if you have 7 posts, limited to 3 posts per page, with an offset of 1, max_num_pages will return 3 instead of 2, which means you will have a blank result for your third page. Here is a fix, which you can apply just before your pagination links:

    $wp_query->max_num_pages = ceil(($wp_query->found_posts - $myOffset) / $postperpage);

    This over-writes the max_num_pages variable after adjusting how many posts were found less your offset. The “ceil” function simply rounds the result up to the next highest integer after dividing the correct number of posts to display by the number of posts you have specified to display on a page.

    • Michael says:

      Greg,

      i’m dropping your code into the content_ nav function not sure this is correct because it’s breaking. If you could help that would be great.

      function twentyeleven_content_nav( $nav_id ) {
      global $wp_query;

      $wp_query->max_num_pages = ceil(($wp_query->found_posts – $myOffset) / $postperpage);

      if ( $wp_query->max_num_pages > 1 ) : ?>
      <nav id="”>


      <?php endif;
      }

  20. hamid says:

    thank you so much for this great tutorial,
    There just a little problem that i can not control in the showposts limit. i only can do it from control panel and not from here $wp_query->query(‘offset=’.$myOffset.’&showposts=5′.’&paged=’.$paged);

    do u have any suggestion?

  21. David (1 comments.) says:

    Unbelievable stuff, man. Worked like a charm. Thanks!

  22. Toby Lewis-Hammond says:

    Basically, you are the most excellent person ever to have graced the face of this fair planet.
    I have now been able to give the wall a rest, by not smashing my head against it anymore.

    You have totally fixed my problem, saved my arse, and made my client very happy.

    Thank you, thank you, thank you!

    Toby :)

  23. Bethany says:

    Wow! A fix that just works like it should! Very nice!

  24. Josh says:

    This is great!! The only problem I have noticed is if the amount of posts equals the postperpage variable you still get the “Next” page link even though you have no additional posts. Any ideas? Thanks,

    Josh

  25. Ankur Oberoi (4 comments.) says:

    Is there any reason this is done in a separate loop than the main loop? will this work if i just try to do it with the main loop on the index.php page?

  26. Bryce (1 comments.) says:

    thank you so much for that, spent hours trying to fix it.

  27. Edille (1 comments.) says:

    Hi,

    You are AWESOME!!! I am having problem with offset and pagination to work at the same time and you save my day :)

    Thank you so much!

  28. sschand88nd (1 comments.) says:

    Offset and Paged together…this is Awesome !!!! Was looking for a solution for the conflict s that occured when i used them together and now your snippet had just saved my day…Thanks a lot :) :)



Trackbacks/Pingbacks

Obviously Powered by WordPress. © 2003-2013

page counter
css.php