Answer the question
In order to leave comments, you need to log in
Incorrectly fulfills pre get posts. How to decide?
I have this code in functiions.php , but 5 posts are displayed on the category pages and not 2, 5 is indicated in the admin panel. If you comment out
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
function custom_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search() ) {
$query->set( 'posts_per_page', 8 );
}
if ( $query->is_category() ) {
if ( wp_is_mobile() ) {
$query->set( 'posts_per_page', 2 );
} else {
$query->set( 'posts_per_page', 2 );
}
}//endif
}//function
add_action( 'pre_get_posts', 'custom_posts_per_page' );
Answer the question
In order to leave comments, you need to log in
So do it
If you comment outif ( is_admin() || ! $query->is_main_query() ) {
return;
}
then on the category page 2 entries are displayed as needed
if ( $query->is_home() ) {
$query->set( 'posts_per_page', 5 );
}
but then on the main page it displays the wrong amount
if ( wp_is_mobile() ) {
$query->set( 'posts_per_page', 2 );
} else {
$query->set( 'posts_per_page', 2 );
}
$query->set( 'posts_per_page', 2 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question