服务热线
15639981097
注:最近24小时 – 是从用户当前的时间算起,往前24小时,这个时间段发布的数量。不一定全部是今天,也有可能是昨天某个时间的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * [get_posts_count_from_last_24h 获取最近24小时内发布的文章数量] * https://www.wpdaxue.com/count-posts-or-custom-post-types-from-last-24-hours-or-from-today.html * @param string $post_type [参数默认为 post 这个类型,你可以填写其他文章类型] */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;} |
将上面的代码添加到当前主题的 functions.php ,然后在你需要调用的地方使用下面的代码即可:
1 | <?php echo get_posts_count_from_last_24h(); ?> |
默认为“post”这个文章类型,如果你要调用其他文章类型,比如 book,可以这样用:
1 | <?php echo get_posts_count_from_last_24h('book'); ?> |
注:今天 – 也就是当天0点-24点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * [get_posts_count_from_today 获取今天内发布的文章数量] * https://www.wpdaxue.com/count-posts-or-custom-post-types-from-last-24-hours-or-from-today.html * @param string $post_type [参数默认为 post 这个类型,你可以填写其他文章类型] */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 ,然后在你需要调用的地方使用下面的代码即可:
1 | <?php echo get_posts_count_from_today(); ?> |
默认为“post”这个文章类型,如果你要调用其他文章类型,比如 book,可以这样用:
1 | <?php echo get_posts_count_from_today('book'); ?> |
燕山网络科技 https://www.hnysnet.com/wordpress/2463.html 转载需授权!
2021-01-11
2020-12-18
2020-12-06
2020-12-06
2020-11-28
您是否只想在指定的时间内展示某些文章/页面内容,例如活动、广告内容、优惠信息?不...
有字库简介有字库是全球第一中文WebFont(网络字体)服务平台。我们致力于美...
扫一扫二维码,添加客服微信
(¬_¬) 还不快点抢沙发!!!