WordPress代码调用24小时内更新的站内文章

如果我们WordPress网站中的文章比较多,尤其是那种新闻资讯类网站可能有需要调用24小时内的文章的。这种采用最新文章调用办法不够精准,我们应该是直接调用24小时内的文章比较合适体验。

WordPress代码调用24小时内更新的站内文章

我们看看代码调用方式。

//调用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;
}

添加到当前主题 Functions.php 文件中。

<?php echo get_posts_count_from_last_24h(); ?>

在需要调用的位置我们调出。我们可以根据需要设置调用文章类型,默认是post。

投票 post
© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享