Codeable info

So You Want to Tidy Up Your Comments?

Posted on by in WordPress Tutorials

The comments section is one of the most important features of any blog. It allows readers and site guests to express their opinions or even ask questions about a particular post. But sometimes the comment list can get pretty messy.

A messy comment section is bad for one rather obvious reason; you want readers to read through your comments, but they won’t readily do so if they have to sort through a list of continuous trackbacks from related posts and alternate sites.

The outlying problem is that WordPress lists all comments, pingbacks, and trackbacks in one place; the comments section. It would certainly be beneficial to completely separate these varying responses from each other. Some WordPress themes already separate trackbacks and comments from each other, and some themes omit trackbacks completely. But for those themes that don’t, here’s how to add in the feature.

The entire process involves altering the current theme’s “comments.php” file. It is highly recommended that you save a backup of this file in case you run into any complications.

Navigate to the WordPress system file editor (Appearance>Editor) and open the “comments.php” file; which is surprisingly listed under the- you guessed it- “Comments” category on the right hand side.

Scroll through the list of code until you find this section:

 <!--?php foreach ($comments as $comment) : ?-->

After you have found the line above, paste in the following directly AFTER (under) it:

<!--?php $comment_type = get_comment_type(); ?-->
<!--?php if($comment_type == 'comment') { ?-->

The finished product should now look like this:

<!--?php foreach ($comments as $comment) : ?-->
<!--?php $comment_type = get_comment_type(); ?-->
<!--?php if($comment_type == 'comment') { ?-->

This basically allows comments to be posted as normal without being interrupted by the pingback or trackback.

Next, you must search through the list of commands until you find the following line:

<!--?php endforeach; /* end for each comment */ ?-->

After you have located the line above, you must paste the following command directly BEFORE (above) it:

<!--?php } else { $trackback = true; } /* End of is_comment statement */ ?-->

The finished product should now look like this:

<!--?php } else { $trackback = true; } /* End of is_comment statement */ ?-->
<!--?php endforeach; /* end for each comment */ ?-->

The above code basically filters the comments from the trackbacks and causes the trackbacks not to appear. The next line of code we enter will retrieve those trackbacks and display them below the comments.

Alas, we have come to the final step, and if it weren’t for copy+paste then it would be a lengthy one (praise be to shortcuts). Navigate through the code until you find the following line:

<!--?php else : // this is displayed if there are no comments so far ?-->

After you have located the command line listed above you are going to paste the following segment (the ENTIRE segment) directly before it:

<!--?php if ($trackback == true) { ?-->
<h3>Trackbacks</h3>
<ol>
<!--?php foreach ($comments as $comment) : ?-->
<!--?php $comment_type = get_comment_type(); ?-->
<!--?php if($comment_type != 'comment') { ?-->
	<li><!--?php comment_author_link() ?--></li>
<!--?php } ?-->
<!--?php endforeach; ?--></ol>
<!--?php } ?-->

All done! You have successfully separated the trackback and comment responses, thus creating a much tidier comment section for your readers. Make sure to save your “comments.php” file before refreshing your blog!

The trackbacks will now display below the comments by default. If you would like to reposition the trackbacks to appear before your reader comments there is a simple modification which can be done. Instead of altering the code we just added to our “comments.php” file you can simply change the “style.css” sheet by adding a division between the comments and trackbacks (

command).

)
Codeable info

Comments (4)

Comment by Glenn says:

Thank you for this!

I tried 7 other sites before I came across yours and none of the code they had available worked for the theme I am running (dailypress).

Appreciate it!

Comment by Briley Kenney says:

Kevin explained it well ^. I apologize. How did the article work out for you after the fix?

Comment by Kevin Muldoon says:

Sorry Jonathan, I need to take some blame for this.
Briley originally posted the code without using the shortcode for displaying code so when he later updated his post with the shortcode he must have forgot to change his code back (when the post was originally wrote it all looked fine).

I hope you enjoyed the article though :)

Kevin

Comment by Jonathan Fouchr says:

You need to do something about your code’s escaping… Seeing < and > instead of is really annoying…

Codeable info