I would like to have a single post on my front page (always the latest), but let normal paging work. So the front page has post 1, the next page should have post 2-11 (1-10 is fine too), then 12-21 or 11-20, and so on. I know I can change the number of posts depending on the context, but setting this to “1” on the homepage means the further pages also show only one post.

My main problem is that /page/2/ and so on works, but /page/1/ always redirects to the real home page, /. This means posts 2-10 are always skipped, since page 2 shows 11-20. I currently solve this by linking to my archive, but this is not ideal when you come to the first posts of the year and there are fewer posts and no obvious way of continuing.

4 s
4

I solved it using the offset query parameter. This allowed me to edit the query in the pre_get_posts hook, and seems to be the cleanest way to do this, without a new query. Now the home page shows only one post, and page/2/ shows posts 2-11. All links keep working, no other modification is required.

add_action('pre_get_posts', 'set_offset_on_front_page');
function _set_offset_on_front_page(&$query)
{
    if (is_front_page() && is_paged()) {
            $posts_per_page = isset($query->query_vars['posts_per_page']) ? $query->query_vars['posts_per_page'] : get_option('posts_per_page');
            // If you want to use 'offset', set it to something that passes empty()
            // 0 will not work, but adding 0.1 does (it gets normalized via absint())
            // I use + 1, so it ignores the first post that is already on the front page
            $query->query_vars['offset'] = (($query->query_vars['paged'] - 2) * $posts_per_page) + 1;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *