Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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

1 Answer 1

up vote 1 down vote accepted

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');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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