2

I want to have the index page to have a different number of posts than the rest of the pages (page 2, page 3 of the index page, as well as the archive pages). In my case I want to limit the number of posts on the front page to 5, while the rest will follow the configuration parameter from wordpress admin (in my case 8).

I followed the example on this page here, and hooked in the pre_get_posts filter as follows:

function limit_posts_per_home_page() 
{
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    if ( is_front_page() && ($paged == 1))
        $limit = 5;
    else
        $limit = get_option('posts_per_page');

    set_query_var('posts_per_archive_page', $limit);
    set_query_var('posts_per_page', $limit);
}
add_filter('pre_get_posts', 'limit_posts_per_home_page');

This 'seems' to work fine. However in actual fact with the posts_per_page parameter set to 8, Page 2 of the index page shows item 9 to 16, rather than 6 to 13, thus missing 3 items.

Is there to modify the offset in some way to make wordpress count from 6 rather than 8 from the 2nd page onwards?

  • While this may not be a duplicate, I think this may give you the answer: wordpress.stackexchange.com/a/105552/21376 – s_ha_dum Sep 15 '13 at 2:59
  • @s_ha_dum Thanks for the pointer. I managed to fix it by setting the offset parameter, which I guess I have to calculate according to the page I am in. Answer below, from some initial tests seems to work. – jbx Sep 15 '13 at 3:18
2

Thanks to s_ha_dum for the tip. I managed to solve it by setting the offset parameter in the special case of 2nd page onwards for the front page as follows:

function limit_posts_per_home_page() 
{
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $first_page_limit = 5;
    $limit = get_option('posts_per_page');

    if (is_front_page())
    {
        if ($paged == 1)
        {
            $limit = $first_page_limit;
        }
        else
        {
            $offset = $first_page_limit + (($paged - 2) * $limit);
            set_query_var('offset', $offset);   
        }
    }

    set_query_var('posts_per_archive_page', $limit);
    set_query_var('posts_per_page', $limit);
}
add_filter('pre_get_posts', 'limit_posts_per_home_page');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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