One of the luxuries offered by WordPress – in addition to the commenting system – is the ability to know when someone has given us a ping or a trackback by mentioning us through another blog and linking back to a given post.

If you’ve ever developed themes, then you’re familiar with the need to display both comments and trackbacks someone in the single post page; however, sometimes you want to separate the two from one another so not to clutter the discussion.

The WordPress API makes this possible in a couple of ways, but there’s one way that I find myself preferring over the others for which I tend to think lends itself to cleaner code, though I’m interested in your feedback on this.

Separate Comments and Pingbacks

The first thing to be clear about, for those who aren’t familiar, is that a “pingback” is a combination of the term “ping” and “trackback” which can be treated as separate entities within WordPress.

With that said, I’ve always grouped them together when working on themes. Specifically, I’ve always placed comments above trackbacks.

Pings and Trackbacks

Separated comments and pingbacks.

Though there are a couple of ways that you can go about doing this, I’ve found that defining two separate functions (which are actually callbacks – more on that in a moment).

First, it’s important to understand the `wp_list_comments()` function. More details are available on the Codex page; however, the arguments most relevant to this particular discussion are the `type` and `callback` arguments.

So given `comments.php`, let’s assume that we want to display our comments and then our pingbacks (as shown in the image above).

In the template file, you’d include something like this (although this – and the rest of the code – is greatly simplified for purposes of example):

Notice in the file above the `type` and the `callback` parameters are different. In one case, we’re displaying comments, in another case, we’re display pings.

Of course, this means that we’re going to need to separate functions used to render the markup for this information. Though there is a little bit of code redundancy, I do find that having multiple functions each dedicated to a specific purpose is a better way to go if for no other reason that keeping functions short and focused (both of which contribute to maintainability over time).

Next, we’ll take a look at the pingback callback. Notice that the initial code isn’t much different, but the template code is. And this is why I think it’s important to separate the functions from one another.

Given the code above, you should see how much cleaner i makes the `comments.php` template file when it’s responsible for making a call to two separate functions each of which serve their own unique purpose within the greater context of the theme.

Though there are other ways in which this can be done, this has been my favorite way of keeping my code separated when rendering information.

Of course, if you’ve got other ways (read: better ways) I’m always up for hearing them!