WordPress网站自定义设置首页文章或栏目页文章显示数的方法
时间:2023年12月10日
/来源:网络
/编辑:佚名
WordPress网站前端的显示文章数是可以通过在后台阅读设置中指定的,后台设置会同步统一应用到首页、列表页、搜索页、标签页和分类页等等。如果我们要实现根据页面类型指定每页显示文章数,那么该如何设置呢?本文和大家一起学习下WordPress后台设置文章显示数以及自定义设置首页文章或栏目页文章显示数。
一、后台设置文章显示数
登录到你的WordPress管理后台,在侧边栏中找到并点击“设置”选项,在“设置”菜单下选择“阅读”。在“阅读设置”页面中,会看到“博客页面至多显示的篇数文章”选项,在这里,可以设置希望在博客页面上显示的文章数量,如图:

图片[1]-WordPress网站自定义设置首页文章或栏目页文章显示数的方法-WordPress建站笔记
输入想要显示的文章数量,并点击页面底部的“保存更改”按钮。
二、自定义设置首页文章或栏目页文章显示数
1、将下面代码添加到当前主题函数模板functions.php中,最终效果是搜索结果页面显示5篇文章,文章归档页面显示10篇。
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;
}
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;
}
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;
}
2、若想实现不同分类显示不同的文章数,修改其中的分类ID,实现指定的分类显示不同的文章数。
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 );
}
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 );
}
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管理后台,在侧边栏中找到并点击“设置”选项,在“设置”菜单下选择“阅读”。在“阅读设置”页面中,会看到“博客页面至多显示的篇数文章”选项,在这里,可以设置希望在博客页面上显示的文章数量,如图:

图片[1]-WordPress网站自定义设置首页文章或栏目页文章显示数的方法-WordPress建站笔记
输入想要显示的文章数量,并点击页面底部的“保存更改”按钮。
二、自定义设置首页文章或栏目页文章显示数
1、将下面代码添加到当前主题函数模板functions.php中,最终效果是搜索结果页面显示5篇文章,文章归档页面显示10篇。
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;
}
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;
}
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;
}
2、若想实现不同分类显示不同的文章数,修改其中的分类ID,实现指定的分类显示不同的文章数。
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 );
}
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 );
}
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跟踪记录访客在网站的搜索词 优化网站内容和搜索体验03-31
- 【wordpress教程】Zibll子比主题的用户中心修改头像增加支持上传PNG图片文件03-31
- 【wordpress教程】wordpress报错429怎么办03-28
- 【wordpress教程】wordpress如何批量删除指定分类目录下的所有文章03-23
- 【wordpress教程】Wordpress 在文章内容的第二段后面插入广告03-03
- 【wordpress教程】WordPress纯代码禁止发表重复标题的文章03-03
- 【wordpress教程】给wordpress博客-VIEU主题评论框添加打字特效(其他主题类似)03-03
- 【wordpress教程】wordpress自动批量定时发布插件 DX-auto-publish02-08
热门文章
- 178Moban源码谈谈免费源码与收费源码的区别
- 2帝国CMS忘记后台登陆用户名、密码、认证码的解决方法
- 3帝国CMS(EmpireCMS) v7.5后台任意代码执行漏洞及具体修复方法
- 4帝国CMS和WordPress 哪个好?哪个适合建站?
- 5如何解决Discuz的密码错误次数过多请15分钟后登陆的问题
- 6帝国cms灵动标签取得内容和栏目链接地址
- 7emlog pro 注册码“开心”教程(如果有一天,emlog官方版 或者 emlog免费版 跑路了,那用户怎么办?)
- 8织梦CMS在nginx下设置伪静态方法(附nginx伪静态规则)
- 9帝国cms后台登录出现”您还未登录”怎么解决?
- 10帝国cms7.5忘记登录密码 和忘记登录认证码以及多次登录 失败被锁定解决办法