
wordpress模板在“设置”->“阅读设置”->“博客页面至多显示”可以全局设置所有列表,tag标签页及首页的文章条数!但是如果需要单独设置的话需要做一些调整!
1、自定义首页、分类、tag标签页文章数(PC端/移动端不同)
<?php
if ( have_posts() ) {
if(strpos($_SERVER['HTTP_HOST'], 'm.**.cn') !== false){
//移动端显示30条
query_posts($query_string . '&showposts=30');
} else {
//pc端显示默认
query_posts($query_string);
}
// Load posts loop.
while ( have_posts() ) {
the_post();
if(strpos($_SERVER['HTTP_HOST'], 'm.**.cn') !== false){
//移动端显示模板
get_template_part( 'template-parts/content/content-m' );
}else{
//pc端实现模版
get_template_part( 'template-parts/content/content' );
}
}
}
?>
2、自定义首页、分类文章数
add_action( ‘pre_get_posts’, ‘zm_set_posts_per_page’ );
function zm_set_posts_per_page( $query ) {
if ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_query’] ) && ( $query->is_search() ) ) {
$query->set( ‘posts_per_page’, 5 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( $query->is_archive() ) ) {
$query->set( ‘posts_per_page’, 10 );
}
return $query;
}
3、自定义不同分类显示不同的文章数
add_action( ‘pre_get_posts’, ‘zm_set_posts_per_page’ );
function zm_set_posts_per_page( $query ) {
if ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(1,2)) ) ) {
$query->set( ‘posts_per_page’, 5 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(3,4)) ) ) {
$query->set( ‘posts_per_page’, 8 );
}
elseif ( ( ! is_admin() ) && ( $query === $GLOBALS[‘wp_the_query’] ) && ( is_category(array(5,6)) ) ) {
$query->set( ‘posts_per_page’, 10 );
}
}
wordpress建站,不要第三方插件实现图片自动添加水印(亲测有效)
wordpress建站,如何实现多个网站调用的文章图片来自同一个文件夹
标签: wordpress优化, wordpress入门, wordpress判断
上面是“wordpress自定义设置首页、分类、tag标签页文章数”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_13762.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

css3如何利用Flex实现响应式表格布局(Flex弹性布局)
DeepSeek官方提示词库
纯css制作卡通头像(随鼠标转头)