"If you can get along with everyone and everyone loves you, then you don't stand for much. A person who stands his ground for his principles and won't compromise his integrity is not loved by everyone."
- Larry Winget

Separate WordPress Comments and Trackbacks Plus a Trackback Counter

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…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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.

  1. It does not display the number of trackbacks in the trackbacks header
  2. 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	#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 &#8220;<?php the_title(); ?>&#8221;</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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#TRACKBACKS EXIST
if($trackback_status)
{
?>
	<h3 class="comment_title"><?php if($trackback_counter == 1) { echo "$trackback_counter Trackback"; } else { echo "$trackback_counter Trackbacks";  } ?> On &#8220;<?php the_title(); ?>&#8221;</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.

Tagged As: , , , , ,

Posted by Vincent on 19 October 2008 12:00 AM in Wordpress Help You can leave a comment, or trackback from your own site. |

Similar Posts

Did You Enjoy This Post?

Subscribe to our blog through our RSS feed or email to receive updates on more posts like this. post on your favourite social networking or media site to let others know about this post. Help us generate more buzz by submitting/voting for this post with the following buttons.

2 Comments On “Separate WordPress Comments and Trackbacks Plus a Trackback Counter”

On 19th November 2008 6:00 AM, TrevinNo Gravatar 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, VincentNo Gravatar said:

Hi Trevin,

The trailing parenthesis in the 2nd code block is continued in the 3rd code block.

1 Trackback On “Separate WordPress Comments and Trackbacks Plus a Trackback Counter”

Post a Comment


(Required)


(Required)