V
V
Vadim Sverdlik2020-05-18 00:18:41
WordPress
Vadim Sverdlik, 2020-05-18 00:18:41

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');

It works, but now it interrupts the number of posts in the footer on these pages, tell me how to competently bypass pre_get_posts in the footer, because. There in general the static quantity of posts needs to be deduced.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2020-05-18
@vadim_sverdlik

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 question

Ask a Question

731 491 924 answers to any question