首页 » WordPress » WordPress 2.7 Separate 留言和样式化留言

WordPress 2.7 Separate 留言和样式化留言

November 3rd, 2008 Denis

上次我讲了如何让你的主题适合 WordPress 2.7 Thread Comments,但是默认实现的 Thread Comments 无法定义 Comments 的格式,也没有把 Trackbacks 分开,并且 Trackbacks 也可以回复,非常无语,那么今天我就讲讲如果,把留言分开成 Comments 和 Trackbacks,和样式化留言。

1. 首先修改你主题的 single.php 文件。把

<?php comments_template(); ?>

函数替换成

<?php comments_template('', true); ?>

上面的修改经让 comments_template 函数创建一个$comments_by_type 的变量。后面我们将会用到。

2. 打开 comments.php 文件,把

<ul class="commentlist">
    <?php wp_list_comments(); ?>
</ul>;

替换成

<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<ul class="commentlist">
    <?php wp_list_comments(array ('type' => 'comment'); ?>
</ul>
<?php endif; ?>

上面这段代码通过指定 type 为 comments 来实现显示的留言都是 comments.

3. 然后在下面添加:

<?php if ( ! empty($comments_by_type['pings']) ) : ?><h2 id="pingback">Trackbacks/Pingbacks</h2><ul class="commentlist">    <?php wp_list_comments(array ('type' => 'pings')); ?></ul><?php endif; ?>

上面这段代码通过指定 type 为 pings 来实现现实的留言都是 Trackbacks.

4. 通过上面三个步骤,已经把留言分成了 comments 和 Trackbacks,但是几个问题依然存在, Trackbacks 竟然也可以回复,Comments 列表不能适应你原有的主题的样式。这个时候我们可以使用 callback 参数来指定一个函数定义留言列表的样式。打开你主题的 functions.php 函数,添加如下两个函数:

5. 自定义 Trackbacks 列表函数。

<?php
//定义 Trackbacks 列表
function custom_pings($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID( ); ?>" class="trackback"><strong></strong><?php comment_author_link(); ?> </li>
<?}?>

6. 自定义 Comments 列表函数

<?php
function custom_comments($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    global $commentcount;
    if(!$commentcount) $commentcount = 0;
    $commentcount ++;
    global $commentalt;
    ($commentalt == "alt")?$commentalt="":$commentalt="alt";   
?>
    <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
    <p class="header <?php echo $commentalt?>"><strong><?php echo $commentcount ?>.</strong> <?php comment_author_link( ); ?> | <?php comment_date( ); ?> at <?php comment_time(); ?> | <a href="#comment-<?php comment_ID( ); ?>">#</a></p>
        <?php echo get_avatar( $comment, 32 ); ?>
        <?php comment_text( ); ?>
        <?php edit_comment_link('Edit Comment','<span class="editlink">','</span>'); ?>
        <?php comment_reply_link(array('depth' => $depth, 'max_depth'=> $args['max_depth'], 'reply_text' => "回复该留言"));?>
    </li>
<?php } ?>

7. 再次打开 comments.php 文件,把下面两个函数修改为:

<?php wp_list_comments(array ('type' => 'comment')); ?>
<?php wp_list_comments(array ('type' => 'pings')); ?>

修改为:

<?php wp_list_comments(array ('type' => 'comment','callback' => 'custom_comments')); ?>
<?php wp_list_comments(array ('type' => 'pings','callback' => 'custom_pings')); ?>

本文主要参考 Separating Pings from Comments in WordPress 2.7

作者:Denis
原文链接:WordPress 2.7 Separate 留言和样式化留言

标签:

分享到 Twitter 分享到 Twitter Delicious 收藏到 Delicious 分享到 Google Reader 分享到 Google Reader 分享到 FriendFeed 分享到 FriendFeed

39 条评论 添加你的评论

Trackbacks/Pingbacks

发表评论

你必须 登陆 之后才能留言。你可以使用以下方式快速登陆我爱水煮鱼:

你也可选择直接在本站 注册


通过博客月赚 1000 美元不是梦 Comment Reply Notification


赞助商

 (mt) Media Temple 长期承接 WordPress 项目 WordPress主机合租 用网页模板,当然要选TemplateMonster China 免費資源網路社群 freegroup.org http://p6p9.com/ 在我爱水煮鱼投放广告位

本站推荐

快速导航