Chat Catcher: Customize Your WordPress Comments and Trackbacks
WordPress themes are almost as varied and numerous as WordPress users. Changing the look of a blog on-the-fly is simple, and if a WordPress theme becomes a little stale, there are plenty of theme galleries to browse. But not all themes are created equal, and even if a theme is done well, WordPress upgrades can leave a theme with outdated code. If you use Chat Catcher on your blog, it’s important to understand how your theme controls the display of comments and trackbacks. More importantly, if you’re not happy with that display, it’s important to learn how to change it.
When I wrote the Chat Catcher plugin, I felt it was important to leave as much of the existing comment system intact. WordPress manages comments effectively and I want to build on the existing code without replacing it. While this is the best approach for Chat Catcher, it does make the solution dependent on whichever theme is in place on a particular blog. For example, if a theme doesn’t display trackbacks and Chat Catcher is still set to post trackbacks, the captured tweets will never appear on the blog. Comments and trackbacks are often formatted differently, so even if a theme displays trackbacks, that doesn’t mean the Chat Catcher comments will be displayed properly.
Although there are many blog posts that address this topic, I thought it best to create one specifically for Chat Catcher. In the following sections, I’ll describe how easy it is to separate comments from trackbacks, and I’ll provide some additional tips for customizing the format of your blog’s comments.
Before You Do Anything
I highly recommend you setup a test blog before reading any further. Test blogs are wonderful things. They protect you from the embarrassment of bringing down your main blog when you make a big mistake performing a simple code change.
Now that I’ve warned you, I recognize that many of you will still apply this to your main blog without testing it first. At least make a copy of your theme files before performing any modifications. We’ll be modifying the comments.php file, so make a copy of that file in your theme folder. For example, let’s say you’re running the default theme on your blog. Go to “\{your web files}\wp-content\themes\default” and make a copy of the comments.php file.
Chat Catcher Settings
There are three main ways to leave feedback on a blog: comments, trackbacks, and pingbacks. All three of these are considered types of comments. They look similar when saved in the WordPress database, but a field called comment_type is used to keep them separate. Each regular comment has a blank comment type; trackbacks and pingbacks have a comment type of “trackback” or “pingback” respectively.
There isn’t an easy way to change a comment’s type after it has been saved. That’s why it’s important to select the comment type for Chat Catcher comments before they’re posted. To select the Chat Catcher comment type, navigate to the Chat Catcher Settings page and select from the list. If your blog receives a moderate amount of regular comments, I recommend separating your Chat Catcher comments so that they do not appear inline with regular comments. Use the Trackback or Custom Trackback (available in Chat Catcher ver. 2.75) type to separate the Chat Catcher comments. It’s important to note that you may need to modify your theme depending on the choice you make. Your readers, however, will appreciate the effort.
Modifying the Comment Display
Open the file called comments.php in your current theme. Comments are often displayed using a “loop” with a “foreach” statement. An example is shown below:
<ol id="commentlist">
<?php foreach ($comments as $comment) : ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<?php comment_text() ?>
<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> — <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
</li>
<?php endforeach; ?>
</ol>
In Version 2.7 of WordPress, a function was added to display the comments without a loop. Although the function is simpler to code, it does make it more difficult to customize the comment layout.
Using wp_list_comments():
<ol class="commentlist"> <?php wp_list_comments(); ?> </ol>
Now that you’ve seen the standard code, it’s time to replace it. Let’s copy the delivered code and change it to separate different comment types. Keep in mind that your existing comments.php may have special formatting or the code may already be set to separate the comments and trackbacks. Make sure you have a backup of comments.php so that you can recreate the original and incorporate theme-specific coding if necessary.
The code displayed below is from a comment loop, modified to separate comments and trackbacks:
<ol id="commentlist">
<?php
$wp_query->comments_by_type = &separate_comments($comments);
$comments_by_type = &$wp_query->comments_by_type;
?>
<?php foreach ($comments as $comment) : ?>
<?php if($comment->comment_type == '') : ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<?php comment_text() ?>
<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> — <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
<ol class="commentlist">
<?php if(count($comments_by_type['trackback']) > 0 || count($comments_by_type['pingback']) > 0) : ?>
<h3>Trackbacks</h3>
<?php endif; ?>
<?php foreach ($comments_by_type['trackback'] as $comment) : ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<?php comment_text() ?>
<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> — <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
</li>
<?php endforeach; ?>
</ol>
This example shows how to separate comments and trackbacks using the wp_list_comments() function:
<ol class="commentlist">
<?php wp_list_comments('type=comment'); ?>
</ol>
<?php
$wp_query->comments_by_type = &separate_comments($comments);
$comments_by_type = &$wp_query->comments_by_type;
if(count($comments_by_type['trackback']) > 0 || count($comments_by_type['pingback']) > 0)
{
echo '<h3>Trackbacks</h3>';
}
?>
<ol class="commentlist">
<?php wp_list_comments('type=trackback'); ?>
</ol>
Save the comments.php file and view a blog post. The comments and trackbacks/pingbacks should be neatly separated.
Separating Chat Catcher Comments
If you selected “Trackback” on the Chat Catcher settings page, the Chat Catcher comments will display under the Trackbacks heading. If you would like to further separate Chat Catcher comments, you can select the “Custom Trackback” type and modify your theme.
Modifying your theme for Custom Trackbacks requires the same type of coding as was used to separate regular trackbacks. Instead of specifying ‘trackback’ in the code, you’ll use a custom type called ‘ctrackback’.
For the loop-style comment display, simply copy the trackback section and change the comment types:
<ol class="commentlist">
<?php if(count($comments_by_type['ctrackback']) > 0 ) : ?>
<h3>Social Media Comments</h3>
<?php endif; ?>
<?php foreach ($comments_by_type['ctrackback'] as $comment) : ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment, 32 ); ?>
<?php comment_text() ?>
<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> — <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
</li>
<?php endforeach; ?>
</ol>
If you are using the wp_list_comments() function, copy the code and change the comment type:
<?php
if(count($comments_by_type['ctrackback']) > 0)
{
echo '<h3>Social Media Comments</h3>';
}
?>
<ol class="commentlist">
<?php wp_list_comments('type=ctrackback'); ?>
</ol>
As you’ve seen, it’s very easy to modify the comment section of WordPress through your theme. Although you usually won’t be forced to make these changes, it’s a good idea to at least consider them. If you don’t feel comfortable making these changes, perhaps the best way to handle this is to ask a friend for help. As a last resort, you can always search for a new theme with better comment handling.
Reference Lookup: Dictionary, Thesaurus, Encyclopedia, & More
powered by PostRef





