How to Separate WordPress Trackbacks and Comments

Posted on July 13, 2009, under PHP, Tutorials, Wordpress,  Bookmark it

In this tutorial I will show you how to separate the trackbacks from the comments in a WordPress Post. I recommend this practice because it makes your blog look professional and the readers will follow the conversations much easily.

I will use the WordPress Default Theme (Kubrik) as the test theme where the changes are made. The code from other WP themes should be pretty much the same.

First, we need to edit the file comments.php, located in the root directory of your theme. Open it and look for the lines:

<!-- You can start editing here. -->

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

AFTER these lines add the following code:

<?php
$trackbacks = array();

foreach ($comments as $comment) {
$comment_type = get_comment_type();

if($comment_type != 'comment')
{
	$trackbacks[] = $comment;
} 

}

$total_tb = sizeof($trackbacks);
?>

The code is useful to count the total trackbacks for the current post. As you can see the variable $trackbacks is declared as an empty array. We use the foreach construct to loop through all comments and check if there are comment types that have the value different than ‘comment’ (could be either ‘trackback’ or ‘pingback’). If there are such comments, we will fill the $trackbacks array. The variable $total_tb is equal to the total trackbacks found. The sizeof function is useful to count the elements of an array.

Let’s display the trackbacks! AFTER the above code add the following lines that will display the trackbacks:

<?php
/* ------- Begin Trackbacks ------- */

if($total_tb)
{
$trackbacks_p = ($total_tb > 1) ? 's' : '';
$trackbacks_text = $total_tb. ' Trackback'.$trackbacks_p;
?>
<h2><?php echo $trackbacks_text; ?></h2>
<olgt;
<?php
foreach ($trackbacks as $comment)  {
?>
<li><?php comment_author_link() ?><br /><?php comment_text() ?></li>
<?php } ?>
</ol>
<?php
}

/* ------- End Trackbacks ------- */
?>

Further on, we need to display the comments. Continue to add the following lines under the ones you have recently added:

<?php
/* ------- Begin Comments ------- */

$comments_number = get_comments_number() - $total_tb;
?>

Find the following code:

	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to &#8220;<?php the_title(); ?>&#8221;</h3>

	<div class="navigation">
		<div class="alignleft"><?php previous_comments_link() ?></div>

		<div class="alignright"><?php next_comments_link() ?></div>
	</div>

	<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>

and replace it with:

<?php
/* ------- Begin Comments ------- */

$comments_number = get_comments_number() - $total_tb;

if($comments_number)
{
if($comments_number == 1)
{
$comments_text = 'One Response';
}
elseif($comments_number > 1)
{
$comments_text = $comments_number. ' Responses';
}
?>
<h3 id="comments"><?php echo $comments_text; ?> to &#8220;<?php the_title(); ?>&#8221;</h3>

	<div class="navigation">

		<div class="alignleft"><?php previous_comments_link() ?></div>
		<div class="alignright"><?php next_comments_link() ?></div>
	</div>

	<ol class="commentlist">

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

	<div class="navigation">
		<div class="alignleft"><?php previous_comments_link() ?></div>

		<div class="alignright"><?php next_comments_link() ?></div>
	</div>

<?php
}
?>

What if the function wp_list_comments() is not used in my theme?

In this case I highly recommend you to use it, since some of the options from the Admin Panel (Settings -> Discussions) are working in conjunction with this function. Click here to find more information on how you can add it. However, if you do not wish to use it look in comments.php for the code:

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

AFTER it insert the following code:

<?php
$comment_type = get_comment_type();

if($comment_type != 'comment') { continue; }
?>

This way only the comments will be shown. Whether your blog is self-hosted or not, be sure your hosts supports this separation. Read WordPress hosting reviews to get more information. Even cheapest website hosting can offer this.

That’s all! Happy coding :D

"Subscribe to the blog"
Receive an update straight to your inbox every time I publish a new article. Your email address will never be shared

Comments

Powered by Facebook Comments

2 Replies to "How to Separate WordPress Trackbacks and Comments"

  1. [...] more: How to separate WordPress Trackbacks and Comments Tags: Comments0 Leave a Reply Click here to cancel [...]

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)