WordPress 2.7将在留言部分大做文章,如AJAX发布留言,留言论坛化等。为了实现这些功能,它将增加一个名为wp_list_comments的函数来代替原有的一些函数。这就将导致现在使用的用于分离留言和pings的技巧将可能失效(例如WeblogToolsCollection的方法)。
这里将针对新的函数对怎样在WordPress 2.7中分离comments与pings做一个解释,以便WordPress主题设计师或其他有兴趣的朋友在WordPress 2.7来临后能制作自己满意的主题。
新方法显示comments的代码将类似这样:
<?php if ( have_comments() ) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>
<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
可以看到现今使用的用foreach得到comment的一串代码,只用一条<?php wp_list_comments(); ?>就搞定了,更加的简单方便(貌似这样自定义性也差了)。
接下来修改上面代码使pings从comments中分离出来:
在single.php(或index.php)文件中搜寻
<?php comments_template(); ?>
将它修改为
<?php comments_template('', true); ?>
这个修改使comments_template生成一个名为$comments_by_type的全局数组,用于下面的分离工作。(现在的comments_template函数没有这个参数项)
打开comments.php,找到
<?php if ( have_comments() ) : ?>
直接在它的下面添加
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
找到
<?php wp_list_comments(); ?>
修改为
<?php wp_list_comments('type=comment'); ?>
最后在
</ol>
的后面添加
<?php endif; ?>
OK,这时留言区就只显示comment而不显示ping了。
在上面的添加的代码后面增加以下代码就能显示ping了
<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<h3 id="pings">Trackbacks/Pingbacks</h3>
<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>
</ol>
<?php endif; ?>
最后完成的代码为:
<?php if ( have_comments() ) : ?>
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>
<ol class="commentlist">
<?php wp_list_comments('type=comment'); ?>
</ol>
<?php endif; ?>
<?php if ( ! empty($comments_by_type['pings']) ) : ?>
<h3 id="pings">Trackbacks/Pingbacks</h3>
<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>
</ol>
<?php endif; ?>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
至此,comments与pings已经分离完毕。
上面的pings将显示ping的地址以及它的内容,如果你想只显示ping的地址而不显示它的内容,你需要进一步地修改上面完成的代码。
打开functions.php(如果没有就在你使用的主题文件夹里创建个吧),添加一个函数
<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>
保存。
重新打开comments.php,找到
<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>
修改为
<ol class="pinglist">
<?php wp_list_comments('type=pings&callback=list_pings'); ?>
嗯,这回真的OK了。
详细原文:http://sivel.net/2008/10/wp-27-comment-separation/
文中提到,WordPress 2.7将可能在2008年11月10号发布!还有一个月。
[...] 另一个有重大更新的是留言的管理。“Quick Edit”,用ajax技术的快捷编辑留言、“Reply”,直接回复该留言(主题经过调整可以以嵌套形式展现)。 [...]
我想问问这样pagenavi是管哪一部份嗫?
这个真的是问倒我了,因为我还没测试过。足够的trackback好像挺难弄到。