pagination - Change Posts per page count

In the wordpress settings => Reading => Blog pages show at most[input field] posts I have it set to 3 posts at t

In the wordpress settings => Reading => Blog pages show at most [input field] posts

I have it set to 3 posts at the moment.

On my index, date archives, tag archives, category archives, search results, etc... All pages that use the loop and paging, it shows 3 posts per page now.

My goal is to be able to have different number of results for different pages. ON my index maybe have 3 posts but on search results or archives, show a different number of results per page.

Any ideas how to do this?

In the wordpress settings => Reading => Blog pages show at most [input field] posts

I have it set to 3 posts at the moment.

On my index, date archives, tag archives, category archives, search results, etc... All pages that use the loop and paging, it shows 3 posts per page now.

My goal is to be able to have different number of results for different pages. ON my index maybe have 3 posts but on search results or archives, show a different number of results per page.

Any ideas how to do this?

Share Improve this question asked Oct 11, 2011 at 2:13 JasonDavisJasonDavis 1,6906 gold badges36 silver badges57 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 30

This will do it: (add to your theme's functions.php)

add_action( 'pre_get_posts',  'set_posts_per_page'  );
function set_posts_per_page( $query ) {

  global $wp_the_query;

  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
    $query->set( 'posts_per_page', 3 );
  }
  elseif ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_archive() ) ) {
    $query->set( 'posts_per_page', 5 );
  }
  // Etc..

  return $query;
}

Improving on the answer above: hook pre_get_posts is fetched by reference, thus it does not require a global call or a returncall.

add_action( 'pre_get_posts',  'set_posts_per_page'  );
function set_posts_per_page( $query ) {

  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
    $query->set( 'posts_per_page', 3 );
  }
  elseif ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_archive() ) ) {
    $query->set( 'posts_per_page', 5 );
  }
  // Etc..

}

Using $GLOBALS['wp_query'] or just $wp_query

add_action( 'pre_get_posts',  'set_posts_per_page'  );
function set_posts_per_page( $query ) {

  if ( ( ! is_admin() ) && ( $query === $GLOBALS['wp_query'] ) && ( $query->is_search() ) ) {
    $query->set( 'posts_per_page', 3 );
  }
  elseif ( ( ! is_admin() ) && ( $query === $GLOBALS['wp_the_query'] ) && ( $query->is_archive() ) ) {
    $query->set( 'posts_per_page', 5 );
  }

  return $query;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745153422a4613968.html

相关推荐

  • pagination - Change Posts per page count

    In the wordpress settings => Reading => Blog pages show at most[input field] posts I have it set to 3 posts at t

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信