- This topic has 15 replies, 6 voices, and was last updated 4 years, 1 month ago by
David.
-
AuthorPosts
-
June 2, 2020 at 1:36 am #1310785
vidueiro
Hi
How Can I limit the number of post on the category? For default appear 10 categories, but when I want use 3 columns the last file appear alone. I would like I can use for example only 9 post.
Thanks
Regards
June 2, 2020 at 6:32 am #1311079David
StaffCustomer SupportHi there,
you can use a PHP snippet like this to change the posts per page for a specific category:
add_action('pre_get_posts', 'custom_posts_per_page'); function custom_posts_per_page($query) { if (is_category( 'category-slug' );) { $query->set('posts_per_page', 10); } }June 2, 2020 at 1:39 pm #1311773vidueiro
But, I would like use in home page, the homepage use all categories and all cateogries. How can I add this code? With hook of elements?
June 2, 2020 at 2:49 pm #1311840Tom
Lead DeveloperLead DeveloperOn the home page, you could do this:
add_action( 'pre_get_posts', function( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( is_home() ) { $query->set( 'posts_per_page', 9 ); } } );The code can be added like this: https://docs.generatepress.com/article/adding-php/
June 3, 2020 at 10:49 am #1313002vidueiro
Now perfect, thanks!!
June 3, 2020 at 4:10 pm #1313313Tom
Lead DeveloperLead DeveloperYou’re welcome 🙂
June 4, 2020 at 8:08 am #1314358vidueiro
A last question, how can I change the number of the posts in a category? The before code and the plugin code snippet don’t run.
June 4, 2020 at 8:21 am #1314385David
StaffCustomer SupportHi there,
you can use the is_category() conditional tag:
https://developer.wordpress.org/reference/functions/is_category/
add_action( 'pre_get_posts', function( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( is_home() ) { $query->set( 'posts_per_page', 9 ); } if ( is_category('your-category-slug') ) { $query->set( 'posts_per_page', 9 ); } } );June 4, 2020 at 8:35 am #1314406vidueiro
yes, ok, but how I do add this code? so that remove when update the theme, can I use any plugin? or in the elements, hook?
June 4, 2020 at 12:55 pm #1314713Leo
StaffCustomer SupportTry one of these methods:
https://docs.generatepress.com/article/adding-php/July 17, 2020 at 6:43 am #1366660Guilherme Lacerda
Thanks, it’s worked!
add_action( 'pre_get_posts', function( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( is_category() ) { $query->set( 'posts_per_page', 9 ); } } );July 17, 2020 at 8:23 am #1366919Leo
StaffCustomer SupportGlad to hear 🙂
July 20, 2020 at 4:20 am #1369450vidueiro
Thanks 😉
July 20, 2020 at 8:40 am #1369826Leo
StaffCustomer SupportNo problem 🙂
March 1, 2022 at 3:43 am #2137637Christian
Hello, which PHP needs to be described here? The Archive.php in the theme?
-
AuthorPosts
- You must be logged in to reply to this topic.