国内外VPS
主机测评与优惠

WordPress首页显示本周和今日更新的文章数量

前言:

我们平时可以看见许多常见的wordpress主题文章底部显示数量。

有人问:“wp主题怎么设置首页更新文章数量呢?”有没有

这一期教程一段代码让你的文章首页出现文字更新数量。

教程步骤一:

源码圈: 将以下代码添加到当前所使用主题的 functions. 文件中:

    // 每周更新的文章数量      function get_week_post_count(){      $date_query = array(      array(      'after'=>'1 week ago'      )      );$args = array(      'post_type' => 'post',      'post_status'=>'publish',      'date_query' => $date_query,      'no_found_rows' => true,      'suppress_filters' => true,      'fields'=>'ids',      'posts_per_page'=>-1      );      $query = new WP_Query( $args );      echo $query->post_count;      }      // 每日更新的文章数量      function WeeklyUpdate() {      $today = getdate();      $query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"]);      $postsNumber = $query->found_posts;      echo $postsNumber;      }

 

PS: 函数

get_week_post_count()是统计每周更新的文章数量并输出

WeeklyUpdate()是统计每日更新的文章数量并输出.

教程步骤二:

将上面的2个函数加到你首页想要的位置:

如下:

  class="gengxin" style="width:100%;height:40px;line-height:40px;text-align:center;box-shadow: 0 0 10px rgba(0,0,0,.05);background-color: #ffffff;margin-bottom: 10px;">      本站共有: style="color:red"> echo $publish_posts = wp_count_posts()->publish;?> 篇文章(其中      WordPress 分类有  style="color:red"> echo get_category(1)->count;?> 篇文章),      本周更新: style="color:red"> get_week_post_count(); ?>  篇,      今日更新: style="color:red"> WeeklyUpdate();?>

其中:

 //输出站点总的文章数量      php echo $publish_posts = wp_count_posts()->publish;?>         //查询特定分类的文章数量,如我要查 WordPress 分类(ID=1)文章数      php echo get_category(1)->count;?>
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《WordPress首页显示本周和今日更新的文章数量》
文章链接:https://www.liuzhanwu.com/15201.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

登录

找回密码

注册