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.

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.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

30 Mar 2010

10 WordPress query_posts tips you probably don’t know

I have written a really brief query_post tutorial before, and it did quite well, but both WordPress and my own skills, have advanced considerably since then. So I thought it would be interesting to revisit the query_posts command and see...
17 Oct 2012

WordPress Social Network Aggregation

I really like the idea of a Tumblog – and even have one on Tumblr.com – but I don’t promote it anywhere. Conceptually it’s great – but I don’t like not having control over my content.What I would really like...
28 Aug 2009

Binary Moon WordPress design vIII

As I briefly mentioned on Monday, I have finally redesigned Binary Moon.I actually started redesigning about 2 years ago. Initially it was going to be an update rather than a totally new look, and I even built most of it,...
04 Nov 2006

WordPress Tips and Tricks – Post in Advance

I’m sure most people know about this but the ability to post things in advance with WordPress is absolutely great. My two blogs (this one and Binary Joy) posted 16 articles whilst I was on holiday despite me not being...