Separate trackbacks and comments (and do it in BlogEngine.NET)

It could be really annoyng when you try to read comments on a blog post with large number of trackbacks. To allow your readers better user experience you can separate trackback from your comments. This short tutorial will show you how to do it in Blogengine.NET quickly (and dirty).

The key is in CommentView.ascx located in \User controls\ subfolder. In original BlogEngine.NET version you have a placeholder for Comments. We are going to add one more - for trackbacks.

<div id="commentlist" style="display:none">
  <asp:PlaceHolder runat="server" ID="phComments" />
</div>
<asp:PlaceHolder runat="server" ID="phTrckbacks" />

And we have to modify code behind. Here is a short description: when adding approved comments we have to ommit trackbacks by excluding all items that contains "pingback" or "trackback" word in Email property. Quite simple. Now we have to add one moreforeach loop and use the same condition to add trackback to newly added placeholder. Take a look at the code below.

string pingbackPath = Utils.RelativeWebRoot + "themes/" + theme + "/PingBackView.ascx";

//Add approved Comments
foreach (Comment comment in Post.Comments)
{
     CommentViewBase control = (CommentViewBase)LoadControl(path);
     if ((comment.IsApproved || !BlogSettings.Instance.EnableCommentsModeration) && (comment.Email != "pingback" && comment.Email != "trackback"))
     {
          control.Comment = comment;
          control.Post = Post;
          phComments.Controls.Add(control);
     }
}

//Add unapproved comments
foreach (Comment comment in Post.Comments)
{
     CommentViewBase control = (CommentViewBase)LoadControl(path);

     if (!comment.IsApproved && Page.User.Identity.IsAuthenticated)
     {
          control.Comment = comment;
          control.Post = Post;
          phComments.Controls.Add(control);
     }
}

//add trackbacks
Panel pnlPingbacks = new Panel();

Literal litTrackback = new Literal();
litTrackback.Text = "<h1 class=\"trackbacks\">Pingbacks and trackbacks</h1><ol id=\"pingbacks\">";
pnlPingbacks.Controls.Add(litTrackback);

int pingbackCount = 0;
foreach (Comment comment in Post.Comments)
{
     CommentViewBase control = (CommentViewBase)LoadControl(pingbackPath);
     if (comment.Email == "pingback" || comment.Email == "trackback")
     {
          control.Comment = comment;
          control.Post = Post;
          pnlPingbacks.Controls.Add(control);
         
          pingbackCount++;
     }
}

As you can see in the code above you load a separate user control to show trackback. This control should be placed in your theme folder. Basically you can copy the code from CommentView.ascx. This is all you will need to place inside PingbackView.ascx:

<li id="id_<%=Comment.Id %>" class="pingback">
    <span class="pingbackDate">
        <%= Comment.DateCreated.ToString("dd MMM yyyy") %></span>
        at <span class="pingbackTime"><%= Comment.DateCreated.ToString("hh:mm") %></span>
    <span class="pingbackAuthor">
            <%= Comment.Website != null ? "<a href=\"" + Comment.Website + "\" class=\"url fn\">" + Comment.Content + "</a>" : "<span class=\"fn\">" +Comment.Author + "</span>" %>
    </span><%= AdminLinks %>
</li>

This is simple but dirty, I know. This is how I did it on my blog. I didn't have a time to create a clean code, but you can always extend and changeit. Would you do this in some other way?

More articles in Blog archive or elsewhere
Advertisement

12 Comment(s)

edzzy

edzzy 19 Dec 2008 #

nice and interesting post here.. keep up the good work.. ;)

Rob Hofker

Rob Hofker 19 Dec 2008 #

I assume that the "dirty" applies to the fact that you are looping three times through the same collections.
I think that this can be tidied up and speeded up a lot by combining the loops into one. Unless the order of the comments (IsApproved && !Moderation vs !Approved && Authenticated) is important then you need two loops.

Anyway another good post showing a simple approach to a real problem that one can use straight away.

Janko

Janko 19 Dec 2008 #

edzzy, thanks.

Rob, yes this is somewhat dirty, but the order is important because unapproved comments should be placed just below the approved, and trackbacks should go to the end. But that's just for the admin, so you actually could use one loop Smile

Matt

Matt 19 Dec 2008 #

Nice tutorial Janko. There are plenty of tutorials out there for Wordpress, so there is no excuse for my site, other than sheer laziness heh.

Jin

Jin 19 Dec 2008 #

I use Wordpress, but I still need to fix my pingback issue. At one point I thought about not displaying them at all, since as you mentioned, they break the flow of reading regular comments, especially if there's a conversation going on among commenters. But I feel if someone's kind enough to write, or link my article, they deserve a backlink from my site.

I'm thinking about having a separate section for pingbacks, and the section collapsed first. I think the design of the ping section really has to do with how much of them you get.

Janko

Janko 19 Dec 2008 #

Matt: it's not that easy to constantly work on the blog especially if you have a lot of other work. I know I'd have to fix some issues on my blog but... one day...

Jin: I like your solution. Or you can, for rexmple, show five trackbacks and add "show all" link after it.

John

John 19 Dec 2008 #

I have been thinking about this myself because I especially hate the snippet of the post in the pingback that has not context and I never know what I am suppose to be reading.  I noticed your layout last week was going to comment on it.  Amazing how you then blogged about it.

Mads Kristensen

Mads Kristensen 21 Dec 2008 #

Nice solution. Instead of checking the e-mail property for "pingback" and "trackback", you could just check if it contain a @. That's how BlogEngine.NET does it internally

Janko

Janko 22 Dec 2008 #

John: cool Smile

Mads: thanks, checking for @ seems to be much simpler.

Ed Kohler

Ed Kohler 23 Dec 2008 #

Interesting tactic. Personally, I intermingle comments and trackbacks, but this is making me rethink whether that's the best way to go.

Yoann. B

Yoann. B 28 Dec 2008 #

Very nice, Thanks !

harrison smith

harrison smith 10 Feb 2009 #

other reason  why blogengine rocks Smile

Comments are closed
via Ad Packs
9292