2013-11-22 75 views
0

我有一個自定義模板,使用循環來抓取發佈信息來建立一個自定義滑塊,它是完美的工作。客戶提出了一個新要求,即他們只希望過去兩天的帖子顯示在滑塊中。WordPress the_date內循環只返回一個職位

在循環中添加以下if語句:

$today = date('j'); 

if (have_posts()) : 
while (have_posts()) : 
the_post(); 

// Added the below code 
    if (the_date('j','','',FALSE)>($today-3)) : 

    // Builds slide 

    endif; 
// End of new code 

endwhile; 

else: 
?> 
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php 
endif; ?> 

在添加該代碼迴路只顯示一個帖子,被發現爲滿足這一IF語句的條件的第一篇文章,並且不顯示其他人。我手動更改了日期,以便至少3個帖子在最近兩天內有發佈日期,但仍然沒有任何內容。

當我取消註釋上述行代碼工作正常,並將所有帖子內置到幻燈片。

我無法使用「posts_where」過濾器,因爲這會影響頁面上的其他帖子(想象一下頁面頂部的滑塊以及滑塊下方的帖子)。

有什麼我失蹤?或者the_date()不能像這樣使用?

回答

0

不要問爲什麼,如果你能解釋一下爲什麼請在評論做,但又不得不做這樣的事情:

$posts_date = $post->post_date; 
$two_days_ago = strtotime(date('Y-m-d H:i:s') . ' -2 day'); 
if ($posts_date >(date('Y-m-d H:i:s', $two_days_ago))) : 
    //process code 
endif; 
2

我有同樣的問題,請嘗試使用

<?php the_time(); ?> 
+0

對不起,認爲我工作,但仍然得到問題,the_time( )不顯示任何內容,但顯示一堆數字 – Eric

0

這是爲我工作 the_time('d/m/Y');

0

難道這是你的問題 - 即按照the_date文檔上的「特殊說明」在http://codex.wordpress.org/Template_Tags/the_date

特別注意:當有同日公佈的下一個頁面上的多個職位,the_date()只顯示後的第一個日期(即the_date的第一個實例())。要重複在同一天發佈的帖子的日期,您應該使用具有特定日期格式字符串的模板標籤the_time()或get_the_date()(自3.0開始)。 用於添加管理界面中設置的日期。

因此,使用get_the_date,如記錄在這裏:http://codex.wordpress.org/Template_Tags/get_the_date

例如

if (get_the_date('j') > ($today-3)) :