I have set 10 posts to display per page from WordPress admin panel, but I want to dispaly 11 posts on first page and in my case its homepage. So I want to display 11 posts on homepage and for all other pages I want to display 10 posts.
I am trying to achieve it with this code but is_home() always return true regardless of page number.
function home_custom_query( $query ) {
if ( is_home() )
{
$query->query_vars['posts_per_page'] = 11;
}
return $query;
}
add_filter( 'pre_get_posts', 'home_custom_query' );
But above code change the number of posts for all pages including homepage.
I also tried this to detect page number and tried as below
function home_custom_query( $query ) {
if ( is_home() && $query->query_vars['paged'] == 0)
{
$query->query_vars['posts_per_page'] = 11;
}
return $query;
}
add_filter( 'pre_get_posts', 'home_custom_query' );
it changes posts per page to 11 for homepage but displays nothing on all other pages.
I also tried the cbnet-different-posts-per-page plugin which fulfills the purpose but it add some extra functionality which I don’t want to use. I need a simple solution for my problem. And I am sure there will be a neat and clean way to solve this issue.
I did it as follows, might be helpful for you as well.