很多网站右侧会显示本周发布了多少篇文章,今天发布了多少篇文章.
对于经常访问你网站的朋友可以告诉他网站的更新情况,不用访客自己去寻找网站更新了多少篇文章.
1. 将以下代码添加到WordPress主题 functions.php 文件中
// 每周更新的文章数量 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; }
其中 get_week_post_count()是统计每周更新的文章数量并输出,WeeklyUpdate()是统计每日更新的文章数量并输出。
2. 将以上两个函数添加到想要输出每周更新和每日更新的文章数量处即可,如添加到首页 index.php 文件中。
这个样式可以按你的样式要求修改.
<div 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;"> 本站共有:<span style="color:red"><?php echo $publish_posts = wp_count_posts()->publish;?></span> 篇文章(其中 WordPress 分类有 <span style="color:red"><?php echo get_category(1)->count;?></span> 篇文章), 本周更新:<span style="color:red"><?php get_week_post_count(); ?> </span> 篇, 今日更新:<span style="color:red"><?php WeeklyUpdate();?></span> 篇 </div>
下面是WordPress主题开发需要用到的解释:
//输出站点总的文章数量
<?php echo $publish_posts = wp_count_posts()->publish;?>
//查询特定分类的文章数量,如我要查 WordPress 分类(ID=1)文章数
<?php echo get_category(1)->count;?>