中文
当前位置: 国外主机测评 » 资源资讯 » 技术教程 » 正文

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

教程前言:

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

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

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

教程步骤一:

源码圈: 将以下代码添加到当前所使用主题的 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;
    }

 

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">php echo $publish_posts = wp_count_posts()->publish;?> 篇文章(其中
    WordPress 分类有  style="color:red">php echo get_category(1)->count;?> 篇文章),
    本周更新: style="color:red">php get_week_post_count(); ?>  篇,
    今日更新: style="color:red">php WeeklyUpdate();?>

其中:

 //输出站点总的文章数量
    php echo $publish_posts = wp_count_posts()->publish;?>
 
    //查询特定分类的文章数量,如我要查 WordPress 分类(ID=1)文章数
    php echo get_category(1)->count;?>
未经允许不得转载:国外主机测评 » WordPress首页显示本周和今日更新的文章数量
0 赞
标签

相关文章