如何分離Comment / Trackbacks / Pingbacks

ben

This site has been moved to dreamerslab.com

本站已經移至 dreamerslab.com

新版的 WordPress2.7評論功能目前均由wp_list_comments()函數來顯示,只需進入後台設定即可馬上呈現評論巢狀層級與時間遞增、減排序的效果,此外WordPress2.7把Comment/Trackbacks/Pingbacks也整合在一起了,或許對某些人來說這是一大福音,但 相對於有特殊用意的人卻是一大困擾。此篇重點在指導使用者如何配合自訂的blog樣板來分離Comment/Trackbacks/Pingbacks

分離Comment/Trackbacks/Pingbacks

1.開啟comments.php尋找以下原始碼

< ?php comments_template(); ?>

修改成

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

2.查看comments.php中的評論顯示循還

< ?php if ( have_comments() ) : ?>

< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”

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

3.利用wp_list_comments函數參數來分離Comment與Trackbacks/Pingbacks顯示循還

Comments loop

< ?php if ( ! empty($comments_by_type['comment']) ) : ?>

< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”

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

Trackbacks/Pingbacks loop

< ?php if ( ! empty($comments_by_type['pings']) ) : ?>

Trackbacks/Pingbacks

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

4.完成comments.php修改如下

< ?php if ( have_comments() ) : ?>
        < ?php if ( ! empty($comments_by_type['comment']) ) : ?>

< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”

    < ?php wp_list_comments('type=comment'); ?>
< ?php endif; ?> < ?php if ( ! empty($comments_by_type['pings']) ) : ?>

Trackbacks/Pingbacks

    < ?php wp_list_comments('type=pings'); ?>
< ?php endif; ?> < ?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/Trackbacks/Pingbacks的分離,趕快到前端頁面看看你的成果吧
眼尖的人應該已經發現,修改後的Trackbacks/Pingbacks呈現方式,有標題、內容、時間,讓人覺得雜亂,下列將修改Trackbacks/Pingbacks樣式以條列式來呈現。

定義Trackbacks/Pingbacks樣式

1.開啟functions.php文件,於最下方添加以下原始碼

< ?php
function list_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment;
?>
	
  • < ?php comment_author_link(); ?> < ?php } ?>
  • 2.修改comments.php

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

    修改成

    < ?php wp_list_comments('type=pings&callback=list_pings'); ?>

    恭喜你,又完成了一項創舉:-)

    接下來,只要利用簡單的方式就可以讓使者用輕易將Comment與Trackbacks/Pingbacks之評論數分離

    1.開啟functions.php文件,於最下方添加以下原始碼

    < ?php
    add_filter('get_comments_number', 'comment_count', 0);
    function comment_count( $count ) {
            global $id;
            $comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
            return count($comments_by_type['comment']);
    }
    ?>

    大功告成!!!

    延伸應用 – 顯示Trackbacks/Pingbacks之數目

    1.開啟comments.php,於適應位置添加以下代碼

    < ?php echo count($comments_by_type['pings']); ?>

    2.完成如下

    < ?php if ( have_comments() ) : ?>
            < ?php if ( ! empty($comments_by_type['comment']) ) : ?>
    

    < ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”

      < ?php wp_list_comments('type=comment'); ?>
    < ?php endif; ?> < ?php if ( ! empty($comments_by_type['pings']) ) : ?>

    < ?php echo count($comments_by_type['pings']); ?>Trackbacks/Pingbacks

      < ?php wp_list_comments('type=pings'); ?>
    < ?php endif; ?> < ?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; ?>

    參考文件

    Separating Pings from Comments in WordPress 2.7
    Numbering your comments, pingbacks, trackbacks or all
    如何分離 Comments 與 Pings
    WordPress 2.7 Trackbacks 的两种处理方法

    Related Posts


    Comments are closed.