[Resolved] Show more posts in one category

Home Forums Support [Resolved] Show more posts in one category

Home Forums Support Show more posts in one category

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1004079
    Sami

    I want to show 30 posts for category 9 and 10 posts for all other categories. By searching the forum I’ve found this code which I edited a little but it somehow doesn’t work, do you have an idea why?

    add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
    function tu_change_posts_per_page( $query ) {
        if ( ( ! is_main_query() && ! is_category('9') ) || is_admin() ) {
            return;
        }
    
        if ( ! $query->is_paged && ! is_category('9') ) {
            $query->set( 'posts_per_page', 30 );
        }
    }
    #1004307
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try this:

    add_action( 'pre_get_posts', function( $query ) {
        if ( ! is_main_query() || is_admin() ) {
            return;
        }
    
        if ( ! is_paged() && is_category( 9 ) ) {
            $query->set( 'posts_per_page', 30 );
        }
    } );

    Let me know ๐Ÿ™‚

    #1004477
    Sami

    Thank you, that works!

    #1004931
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

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