WordPress.org

Ready to get started?Download WordPress

Forums

Posts per page, offset, pagination mystery (1 post)

  1. Datajack
    Member
    Posted 2 years ago #

    Can you display a few posts on one page, then a different number of posts on page 2, page 3, etc.

    I spent a day searching, googled and googled every combination of words and even found other posts saying they did the same thing; still to day there is not answer.

    Can a hero answer this?

    I have a blog category for which I display a very customized "home" page. I display a few posts, in different columns of different sub-categories. And I do a "do not duplicate" trick so they won't be on the next page. I want the next page to display the rest, but more per page.

    Anyway, to put it simple: How to display only, say 5 posts on the first page, and 10 on the others? If you add a custom posts_per_page in function.php for that category only it won't work. Say you set:
    Page 1 : 5 posts per page (posts 1-5)
    Page 2 : 10 posts per page. (posts 11-20 !)
    Page 2 will start at the 11th post because it thinks page 1 displayed 10 posts. Posts 6-9 are skipped.

    So I tried making some code (I'm not a coder) by counting the page I'm on, multiplying the number of posts per page, then subtracting to try to show the post where I should be at. Using offset.

    I made a function in function.php using pre_get_posts that does something similar to this (not the actual code or calculation)

    ------
    If (this category is not paged){
    $query->set ('posts per page', '5')}
    If (this category paged){
    $page = thispagenumber
    $new_offset = (page * 4) - 5
    $query->set ('posts per page', '5')
    $query->set ('offset', $new_offset)}
    ----

    Number of posts per page work, but it still skipped posts. Problem is, Offset doesn't work with pages from what I understand.

    Some have suggested solutions but these may break on the last page and give a 404.

    Is there a non-plugin clear solution to this?

Topic Closed

This topic has been closed to new replies.

About this Topic