Separate WordPress Comments and Trackbacks Plus a Trackback Counter
Posted by admin_mwc on 19 October 2008 12:00 AM in Wordpress Help | 9 Comments
I have read numerous tutorials online about displaying WordPress comments and trackbacks separately. Most of the tutorials seem to be carbon copies of each other and it probably looks something like this…
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>
<?php } else { $trackback = true; } ?>
<?php endforeach; ?>
<?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 } ?>
There are 2 flaws with the code above.
- It does not display the number of trackbacks in the trackbacks header
- It does not hide the comments header if there are only trackbacks
I have addressed the 2 issues in my code and here’s my WordPress theme’s comments.php:
<?php
#COMMENTS EXIST
if($comments)
{
#INITIALIZE COMMENT & TRACKBACK STATUS
$comment_status = false;
$trackback_status = false;
#INITIALIZE COMMENTS COUNTER
$comments_counter = 0;
#INITIALIZE TRACKBACK COUNTER
$trackback_counter = 0;
#DETERMINE COMMENT & TRACKBACK STATUS
foreach($comments as $comment)
{
#RETRIEVE COMMENT TYPE
$comment_type = get_comment_type();
#COMMENT TYPE IS COMMENT
if($comment_type == 'comment')
{
#COMMENTS EXIST
$comment_status = true;
#INCREMENT COMMENTS COUNTER
$comments_counter ++;
}
#COMMENT TYPE IS TRACKBACK
else
{
#TRACKBACKS EXIST
$trackback_status = true;
#INCREMENT TRACKBACK COUNTER
$trackback_counter ++;
}
}
What this code does it looks through all the comments of a particular post to find both comments and trackbacks. I declared a trackback counter variable at the top to count the number of trackbacks, which we will use in another code segment. Whenever a trackback has been detected, we add 1 to the trackback counter variable.
I also want to know if there are any comments for a post. WordPress considers both trackbacks and comments as comments. In this context, we want to consider them as 2 separate things. Therefore, I created variables to check if there are any comments and trackbacks. Sometimes you may have a bunch of trackbacks but no comments. The other guys didn’t write their code to cater for this situation.
#DISPLAY COMMENTS
if($comment_status)
{
?>
<a name="comments"></a><h3 class="comment_title"><?php if($comments_counter == 1) { echo "$comments_counter Comment"; } else { echo "$comments_counter Comments"; } ?> On “<?php the_title(); ?>”</h3>
<div id="comment_block">
<?php
#DISPLAY COMMENTS
foreach($comments as $comment)
{
#RETRIEVE COMMENT TYPE
$comment_type = get_comment_type();
#COMMENT TYPE IS COMMENT
if($comment_type == 'comment')
{
?>
ADD YOUR OWN CODE HERE!
<?php
}
}
?>
</div>
<?php
}
}
The code above displays the comments only if they exist. Please use your existing code to display the comments. I left my code blank at that portion because my templates display comments slightly differently.
#TRACKBACKS EXIST
if($trackback_status)
{
?>
<h3 class="comment_title"><?php if($trackback_counter == 1) { echo "$trackback_counter Trackback"; } else { echo "$trackback_counter Trackbacks"; } ?> On “<?php the_title(); ?>”</h3>
<div id="comment_block">
<ol>
<?php
#DISPLAY COMMENTS
foreach($comments as $comment)
{
#RETRIEVE COMMENT TYPE
$comment_type = get_comment_type();
#COMMENT TYPE IS TRACKBACK
if($comment_type != 'comment')
{
?>
<li><?php comment_author_link() ?></li>
<?php
}
}
?>
</ol>
</div>
<?php
}
The code above displays all the trackbacks in an ordered list. Furthermore, I added a header with a counter. The counter will display 1 Trackback if there is only 1 trackback or X Trackbacks if there is more than one trackback.
That’s all there is to this! Try this out on your WordPress blog and let me know your comments about it. I would be happy to get your ideas on how to improve it further.

Subscribe today to receive my eBook "How to Make WordPress Search Engine Friendly" FREE!



6 Comments On “Separate WordPress Comments and Trackbacks Plus a Trackback Counter”
On 19th November 2008 6:00 AM, Trevin said:
I was just looking into this specific problem, so your blog post is definitely helpful.
FYI — in your second code block, you’re missing a trailing parenthesis to close the IF block off.
On 20th November 2008 1:44 AM, Vincent said:
Hi Trevin,
The trailing parenthesis in the 2nd code block is continued in the 3rd code block.
On 15th March 2009 1:36 PM, Evert Jan said:
Great tutorial. Thanks for posting this.
On 28th July 2010 7:38 AM, Vero said:
Thanks!
With this I managed to correctly count comments and trackbacks in the same area showing comments, but I can not integrate well at the top of the entrance, where you put the typical “published that day by that person, x comments”
That is, I also want the same effect in single.php, archive.php, etc. … in short, in all places where number comments are shows.
Any ideas?
Thanks again!
On 14th September 2010 5:27 AM, Toni said:
Can you elaborate the steps more? I am not good in codes so I don’t know where to begin with. It’s very dangerous to mess with the codes if the steps are not properly explained.
Can you edit this post to a step to step process? I understood what your point about this topic but it lacks step by step explanation for beginners.
On 14th September 2010 9:13 AM, Vincent said:
Hi Toni,
I can’t be more specific with the displaying of the comments because every template is different. If I used my template’s code in the area where I wrote “ADD YOUR OWN CODE HERE!”, and you used the code on your WordPress blog, it may mess up your blog’s layout.
The file you need to edit should be single.php of your WordPress theme. This hack is not very easy to do for people with no programming background.
3 Trackbacks On “Separate WordPress Comments and Trackbacks Plus a Trackback Counter”
Post a Comment