2

I want 5 posts on my front page but 10 posts on subsequent pages (page 2, 3, ..).

In my Settings > Reading > Blog pages show at most > I have 10. But I want to show only 5 posts on the front page, so in index.php I use this :

if ( $paged <= 1 ) $posts = query_posts($query_string.'&posts_per_page=5&paged='.$paged);

Works fine except that.. In page 2, the 6th to 10th posts don't show. As if, from page 2, WP « thought » that the front page has actually displayed the 10 first posts, not just 5.

What can I do ?

4
  • You'll have to write your own code to deal with paging and query offsets in order to have different post per page values on the different pages, it's simply beyond the scope of what queries support in WordPress. I've at least answered similar questions before on the wordpress.org forums though, i'd suggest starting with a search there.
    – t31os
    Commented Jul 16, 2011 at 12:35
  • Any plugin to do the trick ?
    – Mike
    Commented Jul 16, 2011 at 21:57
  • Try <a href="wordpress.org/extend/plugins/custom-post-limits/… Post Limits « WordPress Plugins</a> Commented Dec 24, 2011 at 14:42
  • possible duplicate of Have different number of posts on first page Commented Jul 31, 2015 at 7:12

4 Answers 4

1

There is a post_limits hook that you can use for this purpose exactly:

// in homepage show 6 posts
add_filter('post_limits', 'homepage_limits' );
function homepage_limits( $limits )
{
     if(is_home() ) {
        return  'LIMIT 0, 6';;
     }
 return $limits;
}
-1

Try this:

$page_num = $paged;
if ($pagenum='') $pagenum = 1;
if ($pagenum > 1) { $post_num = 10 } else { $post_num = 5 }
  query_posts('showposts='.$post_num.'&paged='.$page_num); 
    if (have_posts()) : while (have_posts()) : the_post();
    endwhile;endif;
-1

Sorry, just got bored and am trawling through the archives. You could go with this:

if(is_home() || is_front_page) { //some themes forget one or the other
    $post_num = 5;
} else {
   $post_num = 10;
}
query_posts('showposts='.$post_num.'&paged='.$page_num); 
    if (have_posts()) : while (have_posts()) : the_post();
    endwhile;endif;
-1

Try this.

function frontpage_custom_post_count(&$query)
{
    // show specific number of posts on frontpage
    $frontpagePostsCount = 5;
    if (is_front_page() and !is_paged()) {
        $query->query_vars['posts_per_page'] = $frontpagePostsCount;
    }
    // show configured posts on the rest of pages, offsetting the ones showed on frontpage
    if (is_front_page() and is_paged()) {
        $posts_per_page = isset($query->query_vars['posts_per_page']) ? $query->query_vars['posts_per_page'] : get_option('posts_per_page');
        $query->query_vars['offset'] = (($query->query_vars['paged'] - 2) * $posts_per_page) + $frontpagePostsCount;
    }
}
add_action('pre_get_posts', 'frontpage_custom_post_count');
1
  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented May 28, 2013 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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