• Resolved nobita

    (@nobita)


    How to write which is recommended. Please let me know.

    example 1

    use WordPress functions.

    add_action( 'pre_get_posts', 'category_display_per_3' );
    
    function category_display_per_3( ) {
     if ( is_main_query() and is_category() ) {
         set_query_var( 'posts_per_page', 3 );
     }
    }

    example 2

    object access.

    add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
    
    function foo_modify_query_exclude_category( $query ) {
     if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
     $query->set( 'cat', '-5' );
    }

    codex@source

    I am sorry to be poor at English.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to write a pre_get_post()’ is closed to new replies.