Web Directory

How To Highlight Author Comments In Wordpress Without Using Plugins

You’ve seen blogs where the author’s comments are highlighted, and I bet you would have thought that it was pretty cool. Besides the cool factor, highlighting your (authors) comments can be extremely useful as it helps you build your authority factor and helps first time visitors know who the administrator is.

Having seen how useful highlighted author comments were in other blogs, I set out in search for the solution that will let me do the same. Yes, I found many plugins to do the job, however, I don’t like installing Wordpress plugins for unnecessary things like this which can be hard coded into the template.

After looking around a bit I found Matt Cutt’s post really clued me in on what to do. Matt Cutt’s version works, however I think the way I did it below is easier to understand. What I did was to insert a php code into the comments template that checked every comment to see whether the author ID was 1 (It is 1 for me as my author ID is 1, most blogs should leave it at 1) and if it were to print the words authorcomment, if it wasn’t it would print visitorcomment.

Note: I check the comment to the author ID, so if your author ID is not 1, please change the number to your own author ID. Also, this method is not the only method by which you can hard code author highlighting into the template, but this is one of the simplest.

First of all, open your comments.php template, then look for the block of code below. It won’t be exactly the same for different themes, but it should be close enough though.

<?php if ($comments) : ?>
    <h3 id="comments"><?php comments_number(’No Responses’, ‘One Response’, ‘% Responses’ );?> to &#8220;<?php the_title(); ?>&#8221;</h3>

    <ol class="commentlist">
        <!–the beginning of one comment–>
    <?php foreach ($comments as $comment) : ?>

        <li>
            <cite><?php comment_author_link() ?></cite> Says:
            <br />

            <small><?php comment_date(’F jS, Y’) ?> at <?php comment_time() ?></small>

            <?php comment_text() ?>
            <div class="divider"></div>

        </li>

    <?php endforeach; /* end for each comment */ ?>

    </ol>

After that look for the <li> before the start of your comments and replace it with:

<li class="<?php if ($comment->user_id == 1) echo "authorcomment"; else echo "visitorcomment" ?>">

The end result should be below:

<?php if ($comments) : ?>
    <h3 id="comments"><?php comments_number(’No Responses’, ‘One Response’, ‘% Responses’ );?> to &#8220;<?php the_title(); ?>&#8221;</h3>

    <ol class="commentlist">
        <!–the beginning of one comment–>
    <?php foreach ($comments as $comment) : ?>

        <li class="<?php if ($comment->user_id == 1) echo "authorcomment"; else echo "visitorcomment" ?>">
            <cite><?php comment_author_link() ?></cite> Says:
            <br />

            <small><?php comment_date(’F jS, Y’) ?> at <?php comment_time() ?></small>

            <?php comment_text() ?>
            <div class="divider"></div>

        </li>

    <?php endforeach; /* end for each comment */ ?>

    </ol>

OK, now with that done, all you need to do is to assign a style via css to style up all comments with the class “authorcomment”. Open up style.css template, then insert this line:

li.authorcomment{background:#000000;color:#ffffff;}

The line above gives a black background and white text to all of the your comments. If you want to change it, just change the code in between the 2 curly brackets and you’ll be fine.

Go change your theme now to get these cool highlighted author comments :) . Do you guys do it differently? Any suggestions?

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. Wordpress 2.6.2 Has Just Been Released
  2. Wordpress Version 2.6.1 Just Released
  3. This Is How You Should Deal With Trackback Spam
  4. Demonoid Invites Giveaway
  5. Some Changes Coming To Categories and Permalinks
posted in Blogging, Tutorials, Wordpress | | written by admin


No comments yet. Be the first to comment, what a privilege!

Leave a Reply

Visitor Tracker