在這篇文章中,我們將討論如何讓你的主題實現 WordPress 2.7 的評論嵌套功能。
在 Wopus 中文平台新主題-wMagazine 即將完工的時候,我們拿到了 WordPress 2.7 的最新版本 Beta1,第一時間對其進行了詳盡的測試。在 WordPress 2.7 之前,要實現評論嵌套,我們必須開啓 WordPress Thread Comment
這個插件,而在即將發佈的 WordPress 2.7 中,Thread Comment 將被集成進來,使用 wp_list_comments
函數即可調用嵌套評論 (Thread Comment) 。但是,WordPress 開發團隊還沒有任何資料説明如何使用這個新函數,在參考了
default 主題的 comments.php 文件後,我們大致瞭解了其工作流程。也想使自己的主題支持嵌套評論 (Thread
Comment) 這個功能?那就繼續看下去吧。
- 首先,添加 comment-reply JavaScript,讓評論嵌套 (Thread comment) 能夠正常運行
- 判斷 WordPress 版本,有選擇性地使用 wp_list_comments 函數
- WordPress 2.7 的評論 Loop
- 實現嵌套回覆留言
- 當然,我們也可以取消回覆
在 wp_head() 函數之前添加如下函數:
< ?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
WordPress 2.7 通過函數 wp_list_comments 來顯示所有留言,可之前版本並沒有這個函數,所以,我們需在 comments.php 文件中添加如下代碼:
if (function_exists('wp_list_comments')) :
// new comments.php stuff
else :
// old comments.php stuff
endif;
< ?php if ($comments) : ?>
< ?php comments_number('No Comments', 'One Comment', '% Comments' ); ?>
< ?php else : // this is displayed if there are no comments so far ?>
< ?php if ('open' == $post->comment_status) : ?>
< ?php else : // comments are closed ?>
Comments are closed.
< ?php endif; ?>
< ?php endif; ?>
首先需要要把評論框 (Comment Form) 放入一個 ID 為 respond 的 DIV 中,然後並在評論框中添加如下代碼:
< ?php comment_id_fields(); ?>
以上代碼參照 Wopus 中文平台的新主題 wMagazine,請根據自身情況合理安排位置