- This topic has 35 replies, 2 voices, and was last updated 3 years, 7 months ago by Leo.
-
AuthorPosts
-
September 16, 2020 at 4:51 pm #1446461Conor
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
September 16, 2020 at 5:08 pm #1446476LeoStaffCustomer SupportHi there,
Try this snippet:
https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-372928Adding PHP: https://docs.generatepress.com/article/adding-php/
Replace
10
with6
.And set Blog pages show at most to 15 in the readings settings:
https://wordpress.org/support/article/settings-reading-screen/September 16, 2020 at 5:47 pm #1446497ConorHi 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.
September 16, 2020 at 8:07 pm #1446566LeoStaffCustomer SupportSorry 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 the1
in the function.And set Blog pages show at most to 12 in the readings settings:
https://wordpress.org/support/article/settings-reading-screen/September 16, 2020 at 8:10 pm #1446568LeoStaffCustomer SupportTo answer this:
https://generatepress.com/forums/topic/blog-add-on-different-number-of-post-in-first-page-of-blog/#post-1446488Also, 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.
September 17, 2020 at 7:17 am #1447116ConorHi 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..
September 17, 2020 at 8:23 am #1447355LeoStaffCustomer SupportLet’s tackle one issue at a time please.
Does my snippet work for the request of this topic if you remove your snippet first?
September 17, 2020 at 9:11 am #1447419Conorhow can I get rid of the 1 2 3 –> on the bottom. I just want to get rid of it on the home page.
September 17, 2020 at 9:57 am #1447471LeoStaffCustomer SupportTry this CSS:
body.home:not(.paged) .paging-navigation { display: none; }
Adding CSS: https://docs.generatepress.com/article/adding-css/
September 17, 2020 at 10:42 am #1447543ConorI’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.
September 17, 2020 at 10:45 am #1447545LeoStaffCustomer SupportI’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-idSeptember 17, 2020 at 10:55 am #1447556Conoralso, 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
September 17, 2020 at 10:56 am #1447558ConorAlso, 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
September 17, 2020 at 10:57 am #1447561LeoStaffCustomer SupportUsing 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.
September 17, 2020 at 10:59 am #1447563ConorI 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
-
AuthorPosts
- The topic ‘Control posts shown on first page of blog’ is closed to new replies.