WordPress如何调用24小时内更新的文章

追格官方小助手/ 2022年07月08日/ WordPress/ 浏览 1286

如何将WordPress 24小时内更新的文章聚合,只需两步即可。


1、在当前WordPress模板 functions.php 文件中,添加以下代码:


//聚合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;
}


2、在需要展示24小时内更新的文章的位置调用以下代码:


<?php echo get_posts_count_from_last_24h(); ?>


发表评论

暂无评论,抢个沙发...

客服 工单