- This topic has 13 replies, 5 voices, and was last updated 6 months ago by
Leo.
-
AuthorPosts
-
June 2, 2020 at 1:36 am #1310785
Jesus
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); } }
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/June 2, 2020 at 1:39 pm #1311773Jesus
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/
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentJune 3, 2020 at 10:49 am #1313002Jesus
Now perfect, thanks!!
June 3, 2020 at 4:10 pm #1313313Tom
Lead DeveloperLead DeveloperYou’re welcome π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentJune 4, 2020 at 8:08 am #1314358Jesus
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 ); } } );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/June 4, 2020 at 8:35 am #1314406Jesus
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/Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/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 π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/July 20, 2020 at 4:20 am #1369450Jesus
Thanks π
July 20, 2020 at 8:40 am #1369826Leo
StaffCustomer SupportNo problem π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.