如何分離 Comments 與 Pings
在《如何增添 threaded comments 功能》一文中,簡單的提到了我們如何來增添嵌套式評論於舊有的佈景主題上,並且使它相容於 2.7。而這次我們就來看看,應如何將留言評論與 pings 兩者分離成兩個不同的部分。
在開始之前,我們先來看看預設的 comments.php 檔案中的評論迴圈:
<?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; ?>
步驟一
首先,我們先打開佈景主題的 single.php 檔案,找到以下程式碼
<?php comments_template(); ?>
然後,將它置換成:
<?php comments_template('', true); ?>
步驟二
打開佈景主題的 comments.php 檔案,然後將
<ol class="commentlist"> <?php wp_list_comments(); ?> </ol>
置換成
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<ol class="commentlist">
<?php wp_list_comments('type=comment'); ?>
</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 ( ! 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() ) : ?>
<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>
<?php if ( ! empty($comments_by_type['comment']) ) : ?>
<ol class="commentlist">
<?php wp_list_comments('type=comment'); ?>
</ol>
<?php endif; ?>
<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>
<?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 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.php 檔案,將
<ol class="commentlist">
<?php wp_list_comments('type=pings'); ?>
置換成
<ol class="pinglist">
<?php wp_list_comments('type=pings&callback=list_pings'); ?>
然後於 functions.php 檔案中加入
<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>
即可完成。
正常的評論數
如果不想將 Trackbacks/Pingbacks 的數目加到正常的評論數中的話,請將以下的程式碼加進 functions.php 檔案。
<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
global $id;
$comments_by_type = &separate_comments(get_comments('post_id=' . $id));
return count($comments_by_type['comment']);
}
?>