Answer the question
In order to leave comments, you need to log in
What is the correct way to specify pre_get_posts output?
I decided to use pre_get_posts for the first time in order to display a different number of posts, I inserted the following into functions.php:
function custom_posts_per_page($query){
if(is_search()){
$query->set('posts_per_page',12);
}
if(is_category()){
if( wp_is_mobile() ) {
$query->set('posts_per_page',5);
}else{
$query->set('posts_per_page',10);
}
}//endif
}//function
add_action('pre_get_posts','custom_posts_per_page');
Answer the question
In order to leave comments, you need to log in
function custom_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search() ) {
$query->set( 'posts_per_page', 12 );
}
if ( $query->is_category() ) {
if ( wp_is_mobile() ) {
$query->set( 'posts_per_page', 5 );
} else {
$query->set( 'posts_per_page', 10 );
}
}//endif
}//function
add_action( 'pre_get_posts', 'custom_posts_per_page' );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question