Site logo

[Resolved] Limit the number of post

Home Forums Support [Resolved] Limit the number of post

Home Forums Support Limit the number of post

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #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

    #1311079
    David
    Staff
    Customer Support

    Hi 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);
        }
    }
    #1311773
    vidueiro

    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?

    #1311840
    Tom
    Lead Developer
    Lead Developer

    On 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/

    #1313002
    vidueiro

    Now perfect, thanks!!

    #1313313
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

    #1314358
    vidueiro

    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.

    #1314385
    David
    Staff
    Customer Support

    Hi 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 );
        }
    } );
    #1314406
    vidueiro

    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?

    #1314713
    Leo
    Staff
    Customer Support
    #1366660
    Guilherme 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 );
        }
    } );
    #1366919
    Leo
    Staff
    Customer Support

    Glad to hear 🙂

    #1369450
    vidueiro

    Thanks 😉

    #1369826
    Leo
    Staff
    Customer Support

    No problem 🙂

    #2137637
    Christian

    Hello, which PHP needs to be described here? The Archive.php in the theme?

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.