How To Display Different Counters For Comments, Trackbacks, And Pingbacks In WordPress

WordPress

There are three methods to respond to a post in WordPress: create a pingback by linking to the post, leave a trackback or just write a comment. But wouldn’t be nice to have the pingbacks and the trackbacks separated when you displaying the responses to your posts? It’s very pleasant to read comment threads that are not interrupted as well as pingbacks that are well styled. Reading this article you will learns how to separate your responses and show the comment count based on type, so that you can have a well organized comment area. Having separated lists of comments, you’ll have a much cleaner comment area and the comments will be much easier to read. You only have to follow two easy steps and you’re done.

Step 1. How to display separate comments, pingbacks, and trackbacks

Working in WordPress makes it very easy to separate the different response types. Just open the single.php file and call your comments.php template using the following template:

comments_template('/comments.php',true);

Using the $wp_query object you will be able to segregate and count all the different response types . And to be able to separate the comments, pingbacks and trackbacks use the following code as your comments loop in the comments.php file:

<?php if (!empty($comments_by_type['comment'])) { ?>

	<h3 id="comments">Comments<h3>
	<ol>
		<?php wp_list_comments('type=comment'); ?>
	</ol>

<?php } if (!empty($comments_by_type['pingback'])) { ?>

	<h3 id="pingbacks">Pingbacks</h3>
	<ol>
		<?php wp_list_comments('type=pingback'); ?>
	</ol>

<?php } if (!empty($comments_by_type['trackback'])) { ?>

	<h3 id="trackbacks">Trackbacks</h3>
	<ol>
		<?php wp_list_comments('type=trackback'); ?>
	</ol>

<?php } ?>

If your posts has all three types of responses this symmetrical slice of code will output the following markup:



<h3 id="comments">Comments<h3>
<ol>
	<li>Comment #1 - SoftSailor SoftSailor..</li>
	<li>Comment #2 - SoftSailor SoftSailor..</li>
	<li>Comment #3 - SoftSailor SoftSailor..</li>
</ol>
<h3 id="pingbacks">Pingbacks</h3>
<ol>
	<li>Pingback #1 - SoftSailor SoftSailor..</li>
	<li>Pingback #2 - SoftSailor..</li>
	<li>Pingback #3 - SoftSailor..</li>
</ol>
<h3 id="trackbacks">Trackbacks</h3>
<ol>
	<li>Trackback #1 - SoftSailor..</li>
	<li>Trackback #2 - SoftSailor..</li>
	<li>Trackback #3 - SoftSailor..</li>
</ol>

If one of the response types is absent from the post, the markup will omit that section, making the area clean.

Step 2. How to display separate response counts

Now, this is how you display the count for each of the responses types, use the $wp_query object to retrieve the info:

<?php echo count($wp_query->comments_by_type['comments']); ?>

<?php echo count($wp_query->comments_by_type['pingback']); ?>

<?php echo count($wp_query->comments_by_type['trackback']); ?>

Each number corresponding to a specific response type will be displayed by each of the three lines. The response counts may be integrated into the previous code to give the final result which is:

<?php if (have_comments()) : global $wp_query; ?>

	<h2 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' ); ?><h2>

	<?php if (!empty($comments_by_type['comment'])) { ?>

		<h3 id="comments"><?php echo count($wp_query->comments_by_type['comment']); ?> Comments<h3>
		<ol>
			<?php wp_list_comments('type=comment'); ?>
		</ol>

	<?php } ?>
	<?php if (!empty($comments_by_type['pingback'])) { ?>

		<h3 id="pingbacks"><?php echo count($wp_query->comments_by_type['pingback']); ?> Pingbacks</h3>
		<ol>
			<?php wp_list_comments('type=pingback'); ?>
		</ol>

	<?php } ?>
	<?php if (!empty($comments_by_type['trackback'])) { ?>

		<h3 id="trackbacks"><?php echo count($wp_query->comments_by_type['trackback']); ?> Trackbacks</h3>
		<ol>
			<?php wp_list_comments('type=trackback'); ?>
		</ol>

	<?php } ?>

<?php else : // if there are no comments yet ?>

	<?php if (comments_open()) : ?>
		<!-- comments open, no comments -->
	 <?php else : ?>
		<!-- comments closed, no comments -->
	<?php endif; ?>

<?php endif; ?>

If you use this code as your comments loop you will be able to separate the comments, pingbacks, and trackbacks, and each of them will show their number of responses in its heading. The code should be compatible for WordPress 2.7 or better.

How to separate comments from both pingbacks and trackbacks

You could alternately segregate comments from both pingbacks and trackbacks. For this you have to modify the comment loop from Step 1 like this:

<?php if (!empty($comments_by_type['comment'])) { ?>

	<h3 id="comments">Comments<h3>
	<ol>
		<?php wp_list_comments('type=comment'); ?>
	</ol>

<?php } if (!empty($comments_by_type['pings'])) { ?>

	<h3 id="trackbacks">Pingbacks &amp; Trackbacks</h3>
	<ol>
		<?php wp_list_comments('type=pings'); ?>
	</ol>

<?php } ?>

Using the “pings” parameter for the second wp_list_comments loop, it will be ease to group the pingbacks and trackback. To be able to display the count for the pingbacks and trackbacks all combined you have to insert into the Pingbacks & Trackbacks” heading the following snippet:

<?php echo count($wp_query->comments_by_type['pings']); ?>

After finishing the work, your HTML canvas will be clean. Hope that this is useful to you.

WordPress

Do you like this article ?

Sign Up for Free Updates

One Response

  • jhoSeptember 1, 2011 at 06:51

    Hello, nice post.

    but comments_by_type['comments']); ?> did not work in me. I confuse with count() function. Please help…

Leave a Reply

Read previous post:
Diablo 3 Error 37
Diablo 3 Bugs Fixed, Blizzard Apologizes

Three days have passed since Diablo 3 was launched and since then many of the fans who purchased the game...

Close