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 “<?php the_title(); ?>”</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 “<?php the_title(); ?>”</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
![]() |
|
Comments
Powered by Facebook Comments
- July 13, 2009
- article by Gabriel C.
- 2 comments
Related Posts
-
Insert Google AdSense Ads between your WordPress Blog Postsat July 13, 2009 with 22 comments
-
Digging into WordPress: The Book Reviewat May 9, 2010 with 9 comments
-
Construct SQL Query Statements in an efficient wayat December 29, 2008 with 5 comments
-
PHP: Equivalent of trim() Function for Arraysat October 5, 2008 with 2 comments
-
WordPress Template & Theme Generator: Divine Elementeat October 9, 2011




2 Replies to "How to Separate WordPress Trackbacks and Comments"
July 20, 2009 at 1:22 PM
[...] more: How to separate WordPress Trackbacks and Comments Tags: Comments0 Leave a Reply Click here to cancel [...]
August 5, 2009 at 11:16 PM
Thank you for your help!