
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,我们会在看到邮件的第一时间内为您处理!

ArticleRewriterTool:免费无门槛AI文本重写工具,高效优化各类写作内容
Motion Vue:无需编写复杂的命令式代码,轻松实现动画效果
帝国cms标题设置了加粗、颜色等属性在内容页显
Game Worlds:通过简单的文本描述或上传图像,快速生成游戏中的角色及场景
金山打字通官网入口,一个功能全针对性强的打字练习软件
一款支持100多种语言,快速生成语音和视频内容的AI文本转语音平台——Narakeet
Codiga:一款集成到主流IDE和CI/CD流程的AI静态代码分析工具