Wordpress loop show limit posts

后端 未结 5 1924
终归单人心
终归单人心 2020-12-10 15:59

This is the basic loop

I want to show 20 posts on the search results page. I know we can chang

5条回答
  •  死守一世寂寞
    2020-12-10 16:44

    Answers with new query inside template will not work correctly with custom post types.

    But documentation offers to hook on any query, check is it main query, and modify it before execution. That can be done inside template functions:

    function my_post_queries( $query ) {
      // do not alter the query on wp-admin pages and only alter it if it's the main query
      if (!is_admin() && $query->is_main_query()) {
        // alter the query for the home and category pages 
        if(is_home()){
          $query->set('posts_per_page', 3);
        }
    
        if(is_category()){
          $query->set('posts_per_page', 3);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
    

提交回复
热议问题