stadisco
stadisco

Reputation: 557

Different posts_per_page depending on page number

"CODE A" below is used to exclude categories. But I also want to accomplish this:

If is page 1-3, display 8 posts
If is page 4 or more, display 9 posts

How to accomplish this? And how can this be added into "CODE A" ?

.
CODE A

function exclude_categories($query){  
    if ( $query->is_home() && $query->is_main_query() ) {  
    $query->set( 'category__not_in', array(60, 61) );  
    }  
}  
add_action( 'pre_get_posts', 'exclude_categories' );  

Upvotes: 0

Views: 76

Answers (1)

pbaldauf
pbaldauf

Reputation: 1681

I'm not able to test it yet, but this could work

function exclude_categories($query){    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

    if ( $paged < 4 ) :
        $query->set( 'posts_per_page', 8 );
    else :
        $query->set( 'posts_per_page', 9 );
    endif;

    if ( $query->is_home() && $query->is_main_query() ) {  
    $query->set( 'category__not_in', array(60, 61) );  
    }  
}  
add_action( 'pre_get_posts', 'exclude_categories' );  


Perhaps you have to replace

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

with

global $paged; 
$paged = ( is_int($paged) && $paged !== 0 ) ? $paged : 1; 

Upvotes: 1

Related Questions