2011-06-10 23 views
0

變量我試圖改變這種代碼:的strtotime在WordPress的

function filter_where($where = '') { 
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-365 days')) . "'"; 
return $where;} 
add_filter('posts_where', 'filter_where'); 

到這一個:

function filter_where($where = '') { 
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-'.$timestamp.' days')) ."'"; 
return $where;} 
add_filter('posts_where', 'filter_where'); 

你可能注意到了,我試圖把變量$戳裏面的strtotime 。但是代碼不起作用。我是否使用正確的語法將變量放入時間PHP函數中?

我很欣賞任何形式的幫助。

回答

0

這是正確的代碼如果$timestamp包含一個正整數。

然而,在該函數的上下文和沒有global關鍵字,$timestamp是未定義和null

你的意思是傳遞該變量作爲參數and_where()

+0

我已經定義了$ 30時間戳(僅用於測試)。但它仍然不起作用。 我不太明白你的問題,但我只想返回最後$ timestamp天內的帖子數量。 – baby 2011-06-10 02:08:02

+0

哦,我看到了問題。 $ timeframe的值在該過濾器中無法識別。 如何將$ timeframe的值傳遞給該函數? – baby 2011-06-10 02:24:54

+0

@xolomon將它作爲參數傳遞給函數。 – alex 2011-06-10 04:03:00