今天分享一个WordPress分别获取本周/今日/24小时内更新文章数量方法,整理了一下几种文章统计的方式:

获取本周发布的文章数量

//WordPress获取本周发布的文章数量

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

return $query->post_count;

}

使用方法:

将上面获取本周发布的文章数量代码添加到当前主题的 functions.php

<?php echo get_week_post_count()?>

获取今天发布的文章数量

//WordPress获取今天更新的文章数量

function get_posts_count_from_today($post_type =\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) {

global $wpdb;

$numposts = $wpdb->get_var(

$wpdb->prepare(

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”SELECT COUNT(ID) \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”FROM {$wpdb->posts} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”WHERE post_status=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’publish\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”AND post_type= %s \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”AND DATE_FORMAT(post_date, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’%Y-%m-%d\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) = %s\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,

$post_type, date(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’Y-m-d\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, time())

)

);

return $numposts;

}

使用方法

将上面获取今天发布的文章数量代码添加到当前主题的 functions.php ,然后在需要调用的地方使用下面的代码即可:

<?php echo get_today_post_count()?>

上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:

<?php echo get_posts_count_from_today(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’site\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’); ?>

获取最近24小时发布的文章数量

//WordPress获取最近24小时发布的文章数量

function get_posts_count_from_last_24h($post_type =\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) {

global $wpdb;

$numposts = $wpdb->get_var(

$wpdb->prepare(

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”SELECT COUNT(ID) \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”FROM {$wpdb->posts} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”WHERE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”post_status=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’publish\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”AND post_type= %s \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”AND post_date> %s\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,

$post_type, date(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’Y-m-d H:i:s\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, strtotime(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’-24 hours\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’))

)

);

return $numposts;

}

使用方法

将上面获取最近24小发布文的章数量代码添加到当前主题的 functions.php ,然后在需要调用的地方使用下面的代码即可:

<?php echo get_posts_count_from_last_24h(); ?>

上面代码为获取默认为“post”这个文章类型,如果你要获取其他文章类型,比如 site,可以这样用:

<?php echo get_posts_count_from_last_24h(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’site\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’); ?>

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部