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

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

888 ℃

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 );
  }
}  

利用php语法做一个限流访问(排除指定IP)

wordpress建站,不要第三方插件实现图片自动添加水印(亲测有效)

wordpress建站,如何实现多个网站调用的文章图片来自同一个文件夹

利用php语法做一个获取百度热点的接口,并在网站上展示

wordpress在“页面”栏目找不到自己新建的“模板”

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

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

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

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

当前位置: 首页 > wordpress
Trae:新一代免费的AI编程工具

呱呱工具箱

一款免费的在线小工具,无需下载,打开即可使用!呱呱工具箱

在线育儿补贴计算器

快来看看你到底可以领到多少补贴!生活小工具
上一篇:
下一篇:
x 打工人ai神器