web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

wordpress自定义设置首页、分类、tag标签页文章数

239 ℃
     

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的SaaS属性有什么优点

WordPress故障排除大全,网站访问不了解决方法

wordpress获取当前文章ID自定义meta移动端链接

标签: wordpress优化, wordpress入门, wordpress判断

上面是“wordpress自定义设置首页、分类、tag标签页文章数”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_13762.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > wordpress
本文共计1320个字,预计阅读时长9分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款可免费商用圆体字库——江城圆体
下一篇: 推荐一款由字制区设计团队的免费综艺字体——字制区喜脉体
x 打工人ai神器