What the heck is going on, you ask? Say hello to Tersus.

Bare bones? You got it. Tersus is an achingly simple WordPress theme without all the usual cruft. Please pardon the mess. This child theme is still under development.

CD

2007-05-17

Separating Trackbacks/Pings from Comments in WordPress

I was trying to figure out the best way to separate trackbacks/pings from the comments here on the site. I’ve seen a number of tutorials out there, but none seemed to do exactly what I wanted. The code is piece-mealed from a couple of places, but this is how I accomplished it here on the site:

Backup your existing comments.php file associated with your theme! I cannot stress this enough.

Assuming you’re using the default theme, or some variation thereof, you’re going to want to look for this in your comments.php file:

[php]

< ?php if ($comments) : ?>
[/php]

You’re basically going to replace the logic on comments.php from where it says < ?php if ($comments) : ?> (Line 20) to < ?php endif; ?> (Line 61) with the logic below.

Define the number of pings and comments (Source: Sandbox theme):

[php]
< ?php /* NUMBERS OF PINGS AND COMMENTS */ $ping_count = $comment_count = 0; foreach ( $comments as $comment ) get_comment_type() == "comment" ? ++$comment_count : ++$ping_count; ?>
[/php]

Then check to see whether comment_status is set to 'open'. If it is 'open', we then check to make sure the comment_count isn’t equal to zero. Once these conditions are met, I then display only the comments. Some other conditional logic is included, like checking whether the comment’s being held for moderation:

[php]
< ?php if ('open' == $post-> comment_status) : ?>
< ?php if ( $comment_count ) : ?>
< ?php if ($comments) : ?>

Stuff said in regard to this entry:

    < ?php foreach ($comments as $comment) : ?>
    < ?php if (get_comment_type() == "comment"){ ?>

  1. ” alt=”” />

    < ?php comment_author_link() ?> < ?php _e('said'); ?>:

    < ?php comment_text() ?>

    < ?php if ($comment->comment_approved == ‘0’) : ?>

    Your comment is awaiting moderation.

    < ?php endif; ?>

    Posted < ?php comment_date('F j, Y') ?> @
    ‘, ”); ?>

  2. < ?php /* Changes every other comment to a different class */ if ('alt' == $oddcomment) $oddcomment = ''; else $oddcomment = 'alt'; ?>
    < ?php } ?>
    < ?php endforeach; /* end for each comment */ ?>

< ?php else : ?>
< ?php endif ?>

< ?php else : ?>

Hey, there’s no comments on this post!

How about being the first?

< ?php endif /* if ( $comment_count ) */ ?>

< ?php else : // comments are closed ?>

< ?php /* Comments are closed */ ?>

Sorry dude, comments are closed on this post. If you had stuff you liked to say about this entry,
feel free to contact me.

< ?php endif; /* if ('open' == $post-> comment_status) */ ?>
[/php]

Then we do the same thing for trackback/pings:

[php]
< ?php if ( pings_open() ) : ?>
< ?php if ( $ping_count ) : ?>

Trackback/Ping Stuff:

    < ?php foreach ($comments as $comment) : ?>
    < ?php if (get_comment_type() != "comment") : ?>

  1. < ?php comment_date('F jS, Y') ?> at < ?php comment_time() ?> < ?php edit_comment_link('e',' ‘, ‘‘); ?>
  2. < ?php /* Changes every other comment to a different class */ if ('alt' == $oddcomment) $oddcomment = ''; else $oddcomment = 'alt'; ?>
    < ?php endif; ?>
    < ?php endforeach; /* end for each comment */ ?>

< ?php else : /* if ( $ping_count ) */ ?>

< ?php endif /* if ( $ping_count ) */ ?>

< ?php else : // pings are a no-go bro. ?>

< ?php /* Pings are a no-go, bro. */ ?>

Pings are a no-go, bro.

Maybe next time?

< ?php endif /* if ( pings_open() ) */ ?>
[/php]

Save the changes and you should be good to go.

Hopefully this is a pretty straightforward implementation… If you have any better ideas, I’d love to hear ’em.

This item was posted by Chris Harrison.

Tags:

Categories:

Comments are closed.