自定义各个分类每页显示的文章数:
function iteblog_posts_per_page($query) {
$is_home = $query->get('is_home');
if($is_home){
$query->set('posts_per_page', 10);
}
if(empty($is_home)){
if (is_category()) {
$query->set('posts_per_page', 20); } if (is_tag()) {
$query->set('posts_per_page', 30);
} //endif
}
} //function
add_action('pre_get_posts', 'iteblog_posts_per_page');
不同文章类型分类页面显示不同数量文章
function ashuwp_posts_per_page($query){
//industry为文章类型 industyr_category为对应分类法
//is_tax判断是否为分类页面
//is_post_type_archive判断是否为归档页面
//$query->is_main_query使得仅对页面主循环有效
//!is_admin避免影响后台
if((is_tax('industry_category')||is_post_type_archive('industry') )&& $query->is_main_query() && !is_admin()){
$query->set('posts_per_page', 15); //设置为15篇每页
}
return $query;
}
add_action('pre_get_posts','ashuwp_posts_per_page');
不同分类页面显示不同的篇数文章
<?php $posts = query_posts($query_string . '&orderby=date&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
但如果使用了 WP-PageNavi 等分页插件,通常会导致的一个问题是:分页数量是正确的,但访问后面的页数时,会提示 404 错误。
function.php里面添加
function powerxing_custom_posts_per_page( $query ) {
// 为特定分类指定显示数量,键为分类别名(可在后台的分类目录中查看),值为显示数量
$ppp = array(
// 比如为分类 notes 和 gallery 设置不同的显示数量
'notes' => 10,
'gallery' => 15,
);
if ( $query->is_category() && $query->is_main_query() ) {
// 获取当前分类别名
$slug = $query->query_vars['category_name'];
if ( array_key_exists($slug, $ppp) ) {
$query->set( 'posts_per_page', $ppp[$slug] );
}
}
}undefined
add_action('pre_get_posts', 'powerxing_custom_posts_per_page');