Separate comments from trackbacks in your WordPress blog
When your blog starts becoming popular, people will visit, read and talk about it and your blog will have comments pingbacks, and trackbacks.
While everybody knows what is a comment, I am going to explain pingbacks and trackbacks.
From WordPress Codex Glossary:
Pingback lets you notify the author of an article if you link to his article (article on a blog, of course). If the links you include in an article you write on a blog lead to a blog which is pingback-enabled, then the author of that blog gets a notification in the form of a pingback that you linked to his article.
Trackback helps you to notify another author that you wrote something related to what he had written on his blog, even if you don’t have an explicit link to his article. This improves the chances of the other author sitting up and noticing that you gave him credit for something, or that you improved upon something he wrote, or something similar. With pingback and trackback, blogs are interconnected. Think of them as the equivalents of acknowledgements and references at the end of an academic paper, or a chapter in a textbook.
Unfortunately WP’s default theme (and a lot of other themes too) includes comments, pingbacks and trackbacks all together.
It’s not the best solution in my opinion because it would be better to have a main list with only comments, followed by the form to leave a comment and later trackbacks and pingbacks.
In order to do it, we only need get_comment_type() function.
This function returns comment, pingback or trackback according to the type of comment.
So it’s time to edit comments.php file. You will find this file in your theme directory.
Search for
1 | <?php foreach ($comments as $comment) : ?> |
and add below:
1 | <?php if (get_comment_type() == "comment") { ?> |
to determine whether the current comment is a real comment or not.
Then search for
1 | <?php endforeach; /* end for each comment */ ?> |
and add before:
1 | <?php } else { $i_have_trackbacks = true; } ?> |
At this time, you will have your comments list containing only comments, and a boolean variable called $i_have_trackbacks set to true if the script found at least a comment that’s a pingback or a trackback.
Now, after the
1 | </form> |
tag that should close the form used to leave a comment, I repeated the main comment script this way:
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 | <?php if($i_have_trackbacks) { ?> <h3>Trackbacks</h3> <ol class="commentlist"> <?php foreach ($comments as $comment) : ?> <?php if (get_comment_type() != "comment") { ?> <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>"> <?php comment_author_link() ?> on <?php if ($comment->comment_approved == '0') : ?> <em>Your comment is awaiting moderation.</em> <?php endif; ?> <?php comment_date('F jS, Y') ?> <?php comment_time() ?> <?php edit_comment_link('Edit','',''); ?> <div class="commenttext"> <?php comment_text() ?> </div> </li> <?php /* Changes every other comment to a different class */ if ('alt' == $oddcomment) $oddcomment = ''; else $oddcomment = 'alt'; ?> <?php } ?> <?php endforeach; /* end for each trackback */ ?> </ol> <?php } ?> |
As you can see from line 1, the scripts is executed only if I found at least a comment that’s a pingback or a trackback, while at line 9 I am looking for comments that aren’t real comments.
My completed comments.php now looks this way
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | <?php // Do not delete these lines if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!'); if (!empty($post->post_password)) { // if there's a password if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie ?> <p class="nocomments">This post is password protected. Enter the password to view comments.<p> <?php return; } } /* This variable is for alternating comment background */ $oddcomment = 'alt'; ?> <!-- You can start editing here. --> <div id="commentblock"> <?php if ($comments) : ?> <p id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</p> <ol class="commentlist"> <?php foreach ($comments as $comment) : ?> <?php if (get_comment_type() == "comment") { ?> <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>"> <?php comment_author_link() ?> on <?php if ($comment->comment_approved == '0') : ?> <em>Your comment is awaiting moderation.</em> <?php endif; ?> <?php comment_date('F jS, Y') ?> <?php comment_time() ?> <?php edit_comment_link('Edit','',''); ?> <div class="commenttext"> <?php comment_text() ?> </div> </li> <?php /* Changes every other comment to a different class */ if ('alt' == $oddcomment) $oddcomment = ''; else $oddcomment = 'alt'; ?> <?php } else { $i_have_trackbacks = true; } ?> <?php endforeach; /* end for each comment */ ?> </ol> <?php else : // this is displayed if there are no comments so far ?> <?php if ('open' == $post->comment_status) : ?> <!-- If comments are open, but there are no comments. --> <?php else : // comments are closed ?> <!-- If comments are closed. --> <p class="nocomments">Comments are closed.</p></div> <?php endif; ?> <?php endif; ?> <?php if ('open' == $post->comment_status) : ?> <p id="respond"><b>Leave a Reply</b></p> <?php if ( get_option('comment_registration') && !$user_ID ) : ?> <p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in</a> to post a comment.</p></div> <?php else : ?> <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform"> <?php if ( $user_ID ) : ?> <p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Logout »</a></p> <?php else : ?> <p><label for="author">Name <?php if ($req) echo "(required)"; ?></label><br /> <input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="40" tabindex="1" /></p> <p><label for="email">Email Address<?php if ($req) echo "(required)"; ?></label><br /> <input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="40" tabindex="2" /></p> <p><label for="url">Website</label><br /> <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="40" tabindex="3" /></p> <?php endif; ?> <!--<p><small><strong>XHTML:</strong> You can use these tags: <?php echo allowed_tags(); ?></small></p>--> <p><textarea name="comment" id="comment" cols="50" rows="10" tabindex="4"></textarea></p> <p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" /> <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /></p> <?php do_action('comment_form', $post->ID); ?> </form> <?php if($i_have_trackbacks) { ?> <h3>Trackbacks</h3> <ol class="commentlist"> <?php foreach ($comments as $comment) : ?> <?php if (get_comment_type() != "comment") { ?> <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>"> <?php comment_author_link() ?> on <?php if ($comment->comment_approved == '0') : ?> <em>Your comment is awaiting moderation.</em> <?php endif; ?> <?php comment_date('F jS, Y') ?> <?php comment_time() ?> <?php edit_comment_link('Edit','',''); ?> <div class="commenttext"> <?php comment_text() ?> </div> </li> <?php /* Changes every other comment to a different class */ if ('alt' == $oddcomment) $oddcomment = ''; else $oddcomment = 'alt'; ?> <?php } ?> <?php endforeach; /* end for each trackback */ ?> </ol> <?php } ?> </div> <?php endif; // If registration required and not logged in ?> <?php endif; // if you delete this the sky will fall on your head ?> |
And provides a more user friendly way of displaying comments and trackbacks.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.
4 Responses to “Separate comments from trackbacks in your WordPress blog”
Leave a Reply
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.87/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)


(10 votes, average: 3.40 out of 5)





This is really useful; thanks Emanuele!
Thank you. I was wondering how to do this for a long time. Very good work!
Thank you. I was wondering how to do this for a long time. Very good work!
Cool but I cant find the . In my theme they have used wp_list_comments() function or something else if this is not the right one.