Zack Live

Separate Trackbacks from Comments in WordPress 2.9

Since version 2.7, WordPress use wp_list_comments() to display comments instead of the old loop. If you only want to show comments without trackbacks, that’s easy to do, just add a parameter:

wp_list_comments('type=comment');

We also need to consider other things, like re-count the comments, list trackbacks and many bloggers prefer to show trackback links only, without excerpts. These are what we are going to do in this tutorial.

1. Separate trackbacks from comments

First of all, let’s separate trackbacks from comments. Open your single.php, find:

comments_template();

Change into:

comments_template('', true);

This change will generate a global array $comments_by_type for later use.

Open comments.php, find:

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

Below this add:

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

Find:

<?php wp_list_comments(); ?>

Change to:

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

Below the following

(

sometimes), add:

<?php endif; ?>

This part tells WordPress not to show comments heading, if you only have trackbacks.

After that, we will display trackbacks:

<?php if ( ! empty($comments_by_type['pings']) ) : ?>
    <h3 id="pings"><?php _e('Trackbacks/Pingbacks'); ?></h3>
    <ol class="pinglist">
    <?php wp_list_comments('type=pings'); ?>
    </ol>
<?php endif; ?>

2. Style trackbacks

That’s it, trackbacks are separated from comments. Now we want trackbacks to be displayed as a list, we need to add this code to functions.php:

<?php
function list_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>

Back to comments.php, find wp_list_comments(‘type=pings’); we just add, and change it to:

wp_list_comments('type=pings&callback=list_pings');

3. Re-count Comments

The last thing to do is to re-count comments, don’t include trackbacks, just add the following code to functions.php:

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

Comment Navigation Disappeared

I am not sure if this only happen to my theme, I found that after all this modification, my comment navigation code was not working, here is the code:

	<div class="comment-navigation">
		<div class="alignleft"><?php previous_comments_link(); ?></div>
		<div class="alignright"><?php next_comments_link(); ?></div>
	</div>

I didn’t do anything to it. I move this code up to between the

and the , and the comment navigation becomes normal.

[Via: Sivel | QuickOnlineTips | WpBeginner | WpHacks]

Zack Live Free Resource. Zack Live Free Resource. Zack Live Free Resource

Related Posts