如何让你的主题实现 Wordpress 2.7 的评论嵌套功能

This item was filled under [ 一步一步学习中 ]

今天,就让我们来开始告诉你如何使你的主题兼容 Wordpress 2.7 这个专题的第一篇吧。

在这篇文章中,我们将讨论如何让你的主题实现 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)这个功能?那就继续看下去吧。

1. 首先,添加 comment-reply JavaScript,让评论嵌套 (Thread comment) 能够正常运行

在 wp_head() 函数之前添加如下函数:
< ?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
2. 判断 Wordpress 版本,有选择性地使用 wp_list_comments 函数

WordPress 2.7 通过函数 wp_list_comments 来显示所有留言,可之前版本并没有这个函数,所以,我们需在 comments.php 文件中添加如下代码:

if (function_exists(‘wp_list_comments’)) :
// new comments.php stuff
else :
// old comments.php stuff
endif;

###请根据自身情况合理安排位置
3. WordPress 2.7 的评论 Loop

< ?php if ($comments) : ?>
< ?php comments_number('No Comments', 'One Comment', '% Comments' ); ?>
< ?php wp_list_comments(); ?>
< ?php previous_comments_link() ?>
< ?php next_comments_link() ?>

< ?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; ?>

4. 实现嵌套回复留言

首先需要要把评论框 (Comment Form) 放入一个 ID 为 respond 的 DIV 中,然后并在评论框中添加如下代码:
< ?php comment_id_fields(); ?>
5. 当然,我们也可以取消回复

< ?php cancel_comment_reply_link(); ?>

Bookmark and Share
Tagged with: [ ]

喜欢这篇文章的人还喜欢。。。

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Comment