簡體   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