我解決了我自己的問題,但它花了我一段時間。各種簡單的方法不得不因某種原因被丟棄。最終我不得不使用wp_comment_query。
這樣的事情會像MySQL查詢非常直觀的(這是我最熟悉的語言):
從some table
選擇timestamp
其中timestamp
> ( 從some table
選擇timestamp
其中comment id
= This_Comment_ID ) 作爲some alias
和comment id
> This_Comment_ID 爲了通過time stamp
升序 限制1;
但WordPress引發了各種各樣的障礙。對於第一件事,WordPress date_query允許關於日期的「之後」定義...但不是時間。所以我們不能使用它。相反,「timestamp
>」部分已經被寫成這樣:
'date_query' => array(
'year' => date('Y' , strtotime($written_stamp)) ,
'month' => date('F' , strtotime($written_stamp)) ,
'day' => date('j' , strtotime($written_stamp)) ,
'hour' => date('H' , strtotime($written_stamp)) ,
'minute'=> date('i' , strtotime($written_stamp)) ,
'compare' => '>='
),
其中$ written_stamp是當有人評論提交的時刻。即使該時間戳也必須從get_comment_date()加get_comment_time()連接起來。
更糟糕的是,Wordpress的查詢不允許與評論ID進行比較。我不能簡單地寫「comment id
> This_Comment_ID」。僅用時間戳> =當前註釋的時間戳來查找註釋是不夠的。因爲那將包括當前評論 - 如果在同一分鐘內提交超過1條評論,可能還有其他評論。因此,要打破平局和排除這實際上是早先的評論,我不得不一些非常不雅行添加到WordPress的查詢,具體exluding半打最近的評論直至幷包括有問題的評論:
'comment__not_in' => array(
$this_comment ,
$this_comment - 1 ,
$this_comment - 2 ,
$this_comment - 3 ,
$this_comment - 4 ,
$this_comment - 5
)
其中$ this_comment = get_comment_id()。
然後,似乎WordPress的查詢被設計爲返回數組中的一大堆評論 - 不只是1條評論的信息,這是我需要的。所以我無論如何都經歷了一次foreach,因爲這是顯而易見的。
簡單的代碼。但不必要的難以想出。
所以 - 如果我們回答*這個問題* - 關於如何獲取數據,並參考它 - 是一個「完整」的答案給你?或者,你是否也想知道如何將它放到你的模板中? –
你使用哪個主題?你是否希望在新模板中做到這一點?或者,修改現有的模板?您是否熟悉[wp_list_comments](https://codex.wordpress.org/Function_Reference/wp_list_comments)? –