Archived topic
Control posts shown on first page of blog
35 replies · Started by Conor on September 16, 2020
Hi, so my first page of my blog is my home page, and I only want 6 of my sticky posts showing up on my home page. Now for the rest of my pages, I am comfortable with 15 posts showing up on each page. How can I control how many posts show up on my blog, and how can I make it different for the home page then for the other pages of my blgo? thanks in advance, C
Hi there,
Try this snippet:
https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-372928
Adding PHP: https://docs.generatepress.com/article/adding-php/
Replace 10 with 6.
And set Blog pages show at most to 15 in the readings settings:
https://wordpress.org/support/article/settings-reading-screen/
Hi Leo, it sort of works for me.
It doesn't work with this snippet
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_home() ) {
$query->set( 'posts_per_page', 15 );
}
if ( is_category('your-category-slug') ) {
$query->set( 'posts_per_page', 15 );
}
} );
See, I want 6 on first page then I want 12 on all the other pages.
ALso, how can I make it so that the numbers disapear on the home page? I don't want the ' 1 2 3 next -> ' on the home page.
Sorry can you confirm that this is what you are adding?
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
function tu_change_posts_per_page( &$query ) {
if ( ! is_main_query() ) {
return;
}
if ( ! $query->is_paged ) {
$query->set( 'posts_per_page', 6 );
}
}
It should make the first page to display 6 posts. Don't change the 1 in the function.
And set Blog pages show at most to 12 in the readings settings:
https://wordpress.org/support/article/settings-reading-screen/
To answer this:
https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-1446488
Also, when you run the snippet, just make it run on the front-end only. Or does this cause some other issue I am unaware of?
I usually run it everywhere but if front end works for you then it should be fine as well.
Hi Leo, yes that is what I added, but it doesn't work well with the other function that was already on my blog.
add_action( ‘pre_get_posts’, function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_home() ) {
$query->set( ‘posts_per_page’, 15 );
}
if ( is_category(‘your-category-slug’) ) {
$query->set( ‘posts_per_page’, 15 );
}
} );
Also, how can I get rid of the 1 2 3 --> on the bottom. I just want to get rid of it on the home page..
Let's tackle one issue at a time please.
Does my snippet work for the request of this topic if you remove your snippet first?
how can I get rid of the 1 2 3 –> on the bottom. I just want to get rid of it on the home page.
Try this CSS:
body.home:not(.paged) .paging-navigation {
display: none;
}
Adding CSS: https://docs.generatepress.com/article/adding-css/
I'm having an issue that if I publish a new article - it goes on the first page of the blog.. it seems that with the code :
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
function tu_change_posts_per_page( &$query ) {
if ( ! is_main_query() ) {
return;
}
if ( ! $query->is_paged ) {
$query->set( 'posts_per_page', 6 );
}
}
It shows 6 of the most recent posts plus any sticky posts. How could I get it to just show 6 posts, not 6 of most recent posts plus all sticky posts?
Maybe a piece of code that get's it to show just the 6 most recent stickied posts.
I'm not sure if we can modify the code that way - the theme itself doesn't control the post loop from WordPress.
I think the better/easier solution would be to use WP Show Posts on the front page:
https://en-ca.wordpress.org/plugins/wp-show-posts/
Then you can select whichever 6 posts you want to show in the list using the post IDs:
https://docs.wpshowposts.com/article/more-settings-overview/#post-id
also, can I set it so that it is only 6 posts on front page, but a different amount of posts on category pages? I want 6 posts on first page, but am happy with 15 on all other post-showing pages of the site.
I tried this but it didn't work
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category('your-category-slug') ) {
$query->set( 'posts_per_page', 15 );
}
if ( is_home() ) {
$query->set( 'posts_per_page', 6 );
}
} );
this code still just set every page to 6 posts
Also, something that would work is if I could just set it so only sticky posts showed up on front page.. if i can set it to just sticky posts on front page, I could figure out the rest.
C
Using WP Show Posts plugin for the front page would be the best option.
Then you can use the filter to control the category pages only.
I looked at it and it looked a little complicated and time-consuming to set up. Is it impossible to set the front page to only show sticky posts? because if it is, then I could fix the rest from that.. If the code ins't possible for that, I'll try the wp show posts