繁体   English   中英

只获得今天的帖子?

[英]Get only today posts?

我需要一个能够回应今天发布的帖子的功能。

从00.00到23.59。 我已经创建了一个函数,但它没有给出我需要的结果是我的函数:

function todayNews() {
    $id = mysql_real_escape_string ($id);
    $sql = 'SELECT * FROM wp_posts WHERE  post_status = "publish" AND post_date = CURRENT_DATE  ORDER BY post_date DESC LIMIT 15';
    $res = mysql_query($sql) or die (mysql_error());

if (mysql_num_rows($res) !=0):
    while ($row = mysql_fetch_assoc($res)) {

    $title = ($row['post_title']);

    $old_date = $row['post_date'];             // returns Saturday, January 30 10 02:06:34
    $old_date_timestamp = strtotime($old_date);
    $new_date = date('H:i', $old_date_timestamp);

    $mycontent = ($row['post_content']);
    $mycontent = strip_tags($mycontent);
    $mycontent = substr($mycontent,0,350);
    $mycontent = preg_replace("/\[caption.*\[\/caption\]/", '', $mycontent); 

    $first_img = '';
    $my1content = $row['post_content'];
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
    $first_img = "/img/default.png";
    }

    echo '


    <ul class="li-sub">
                <li>
'.$title.'
</li>

        </ul>

    ';  
}
    else:
        echo 'There are no posts for today !';
    endif;

} // end  

谢谢 !

编辑:Post_date有这种格式:Ymd H:我

您将POST_DATE(日期时间)与CURRENT_DATE(日期)进行比较,它不会给出任何结果。

您可以尝试以下SQL

$sql = 'SELECT * FROM wp_posts WHERE  post_status = "publish" AND DATE(post_date) = CURRENT_DATE  ORDER BY post_date DESC LIMIT 15';

这将在与当前日期比较之前将post_date转换为仅日期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM