我们使用WordPress建网站,每个分类每页的文章数量是固定的,全部受后台的控制。但在实际做网站时,往往需要不同的分类下每页显示不同的文章,然后进行分页。

怎么实现在自己做网站时不同的分类不同的文章数量分页呢?方法很简单,只需要按照以下的步骤控制即可。
function filter_pre_get_posts( $query ){
if ( $query->is_main_query() ){
$num = '';
if ( is_category(array(9)) ){ $num = 14; }
//if ( is_category(array(10)) ){ $num = 14; }
//if ( is_category(array('questions')) ){ $num = 14; }
// if ( in_category(array('jhg','hjj','yjj','xxj','jlg')) ){ $num = 10; }
//if ( is_home() ){ $num = 10; }
// else if ( is_category() ){ $num = 10; }
// else if ( is_tag() ){ $num = 10; }
// else if ( is_date() ){ $num = 10; }
// else if ( is_author() ){ $num = 10; }
// else if ( is_search() ){ $num = 10; }
// else if ( is_archive() ){ $num = 10; }
if ( '' != $num ){ $query->set( 'posts_per_page', $num ); }
}
return $query;
}
add_action('pre_get_posts', 'filter_pre_get_posts');