Skip to content

WordPress tips and tricks – comments, trackbacks, and query_post

Over the last year and a bit Binary Moon has slowly evolved. Rather than redesign my site every few months I have chosen to update and improve as I go. Doing this I have added a few little features that I think are quite cool, so thought I would share my code in case you find it handy as well.

You have left a comment…

This ones easy. When people leave comments you want to show them a small message letting them know the comment is in moderation so that they don’t repost. All you have to do is, in your comment loop, add something like this:

<?php if ( $comment->comment_approved == '0' ) { ?>
    <p class="alert">Thanks for your comment. It is currently in the moderation cue and will be visible for everyone to read as soon as I have verified it</p>
<?php } ?>

I must admit I find it a bit strange that this works, since I would have thought everyone would see the message, but it appears to work fine, so I’m not going to complain :)

Trackbacks and Comments

This one was also fairly simple to implement. I spent a bit of time Googling for ways to do this and ended up finding a couple of different techniques but the code on all of them was really untidy (sorry). I’m a big fan of beauty in code, not because I’m a geek (although I am) but because I find it makes the code easier to understand and maintain.

I ended up using the system on xmouse as a template for my code.

What you need to do is add the following code to functions.php (this is only available in wordpress 2.0 and greater – if you’re not using it I recommend you upgrade)

<?php
$bm_trackbacks = array();
$bm_comments = array();

function split_comments( $source ) {

    if ( $source ) foreach ( $source as $comment ) {
    
        global $bm_trackbacks;
        global $bm_comments;

        if ( $comment->comment_type == 'trackback' || $comment->comment_type == 'pingback' ) {
            $bm_trackbacks[] = $comment;
        } else {
            $bm_comments[] = $comment;
        }
    }
} ?>

What the code is doing is looping through all the comments and splitting them into two new variables which are separately the comments and trackbacks for the post.

To use it you add the following before your comment loop:

<?php
global $bm_comments;
global $bm_trackbacks;

split_comments( $comments ); ?>

And then you can use the new comments the same way you always have:

<?php

    // --------
    // COMMENTS
    // --------

    foreach ( $bm_comments as $comment ) {
        // comment display code (exactly the same as you use at the moment)
    }

    // ----------
    // TRACKBACKS
    // ----------

    // check there actually are some trackbacks
    if ( count( $bm_trackbacks ) > 0 ) {

        // I like to add a separate heading here
        
        foreach ( $bm_trackbacks as $comment ) { ?>
            // trackback display code (I only display a link to the site and no message content)
        }
    
    }

?>

Now that I have typed all that out it looks a lot bigger and more complicated than I intended. Hopefully it’s clean enough for someone to get some use from it.

Query Post

WordPress has a command called query_posts that lets you do all sorts of groovy things with the site content. The query posts command is how I create my Recent Posts and Link Blog listings at the bottom of my homepage.

Rather than try and reinvent the wheel I am going to point you to Phu from ifElse who’s blog posts helped me work out what was going on with this most helpful of commands (cheers Phu).

While you’re there you should check out his very cool new site design.

Your turn…

If you have any hints and tips for cool little features that you think would benefit my, or anyone elses, website then I’d love to hear them.

Share

Join me, Dawn, Rich Schmidt, Josie. They're all chatting about "WordPress tips and tricks – comments, trackbacks, and query_post" below ›

Read Comments

37 Comments »

  1. Thanks a lot for this post! I’ve already used two of the ideas (comment response and query_posts) at a site I’m developing for a community event. It’s not a blog, but it uses WP! It’s the site I linked to with this comment.

    Thank you!

  2. I’m trying to separate my trackbacks and feedbacks but dude, the instructions above make no sense at all.You should specifically write out where all that coding should go.Should it all go in the functions.php file or is some of that meant for the comments.php file of your theme? What goes where is just not explained well and now my head hurts :(

    :)

  3. Pingback: LP3Records » Tips and Tricks….
  4. I must admit I find it a bit strange that this works, since I would have thought everyone would see the message, but it appears to work fine, so I’m not going to complain

    Well if the comment hasn’t been moderated it won’t appear to anybody but the author, therefore the message will display to everyone who sees it, it’s just that that is only the author.

  5. Josie, I agree, this is a little difficult to follow. For separating comments from trackbacks, from what I understand, add the first block of code to the functions.php file, and the next two blocks of code are to replace the comment functions in your comments.php or comments-popup.php, whichever you are using, in your WordPress Theme. This is great but it does need to be a little clearer for the less code experienced.

    Another note needs to be added to warn people that if they upgrade WordPress, they lose this function completely as the functions.php will be overwritten. Therefore, a warning to everyone to backup their functions.php and make sure they make plenty of notes about the changes so they can put them back in after an upgrade.

    I use a more cumbersome technique, but all of the code goes into the WordPress Theme, which separates it from the core programming and out of harm’s way from upgrades. Too bad this beautiful coding can’t be set right into the Theme template files. Is it possible?

  6. This article wasn’t written for less experienced users. It was assumed people wanting to do this sort of thing knew what they were doing. I should have made that clear.

    As for functions.php it doesn’t get overwritten on upgrade since it IS a theme file and only gets changed if you update a theme – again I should have made that clear. Not being a wordpress programmer I wasn’t aware there was a functions.php as part of the wordpress core.

    All of the tips in this article are designed to be usable when you upgrade without any problems (assuming the wordpress internals don’t go and change drastically).

  7. Pingback: Ryan’s Life » Separate Trackbacks and Comments in Wordpress
  8. Pingback: apexad.net » Blog Archive » Separate Trackbacks and Comments in Wordpress
  9. Pingback: apexad.net » Blog Archive » I am using Ella now!
  10. Pingback: Ryan’s Life » Blog Archive » Separate Trackbacks from Comments Revisited
  11. Pingback: blogu’ lu’ ciops » Blog Archive » Wordpress: Separati trackbacks de comentarii
  12. Hi Ben, thanks very much for this – took me a while to find it, but was exceptionally useful when I did! Was rather disappointed to find no real mention of a similar technique in the WordPress documentation.

    Much appreciated!

  13. Can someone tell me how to include the number of blog views (like in blogspot.com)… wordpress.com doesnt’ have a feature.. can someone give me any code known to show this… just curious how popular one can get…
    thanks…

  14. I’d love to use this and consider myself fairly experiences, but I struggled to get it to work without destroying my comments file. Is the plugin available yet?

    here’s hoping *crosses fingers*

  15. I downloaded and installed it straight away, I promise I won’t tell! And it seems to work with 2.5

    Thanks – I think I have all your plugins now :-)

    Dawn

  16. Dawn – that’s great – I’m glad it works. I am going to write about it soon so you don’t have to remain silent for long ;)

  17. thanks for the tips and tricks. I have updated my 404 and added some new page templates. Such a simple thing to do and I would probably never have found it if it wasnt for this site.

  18. Hi there,
    Know anyone which plugin to use if I want only the first part (first paragraph) of the article to be displayed? If someone want to read more there will be a “read more” link…
    On my blog right now is displayed all of article content and I don’t want this…
    I’m talking about the latest 10 articles displayed on the first page of my blog.
    I’m using WordPress.
    Can anyone help?

  19. I was curious where do I add in the comment loop, the message notifying individuals that their comments are being notified? Thank you!

  20. I have added the moderation comment message code but do not know what to right in place of trackback display code. What to write there? just trackback function?

  21. Thanks so much for this – dead straightforward and works like a charm.

    Props for making it elegant, too. You’re not wrong about much of the code floating around out there being a mess.

Comments are closed. Let's continue the conversation on Twitter.
css.php