How to Highlight Author’s Comments

There are 2 ways to highlight author’s comments, one is use highlight author comments wordpress plugin, another idea is without wordpress plugin. If you don’t want to use wordpress plugin to highlight author’s comment, you can follow the below few simple steps.

First, you need to open styles.css and add the following into this file.

.authorstyle { background-color: #B3FFCC !important; }

Second, open comments.php, find and locate the following code.

<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"></li>

And then replace with

<li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>"></li>

Please note that you must change 1 to the user id of the author. Thanks to Matt Cutts for this tutorial.

How to Display Recent Posts from Specific Category

Initially I was thinking to display all the posts from specific category in my archives page, but this doesn’t work well if you have thousand of posts for one category, that’s why I want to limit the number of recent posts displayed from specify category.

To display recent posts from specific category, you can make use of the following coding. To change the number of displayed recent posts, simply change the showposts=10 to your desired number; cat=1 is your category number, you have to change this too.

<ul>
<?php $recent = new WP_Query("cat=1&amp;showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul

In case you need to display all posts from specific category, then you can use the following coding. cat=6 is your category number, please change it.

<ul>
<?php query_posts('cat=6'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $wp_query->is_home = false; ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> – <?php the_time('j F Y') ?></li>
<?php endwhile; endif; ?>
</ul>

Thanks to Ready WP Themes for this coding.

How to Add Avatar to Your Comments

The default wordpress themes are came with avatar support. In case you have use some other free or premium wordpress themes with no avatar support, then you can adding the following simple and short coding to your comment.

<?php echo get_avatar( get_the_author_email(), '80'); ?>

get_the_author_email outputs the post author’s email and the “80” is the size of the avatar image in pixels, you can change the size of the avatar according to your needs. Is it looks simple? @@

How to Display Post Words Count

I don’t know why some people like to display post words count on their blog, but I personally don’t like this function. In case you are the ones like this function, then you can copy and paste the following coding to functions.php.

function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}

And then you can call the function to get the number of words of the current post:

<?php echo wcount(); ?>

Thanks to Digging Into WordPress for this excellent code.

How to Display Recent Comments Without a WordPress Plugin

Get Recent Comment wordpress plugin shows excerpts of the latest comments, it also can (optionally) separate the trackbacks/pingbacks from the comments.

If you don’t want to use any wordpress plugin to display recent comments, you can copy the following coding and paste it to the sidebar.php file. To change the number of displayed comments, simply change the LIMIT “10″ to your desired number.

<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "\n<ul>";
foreach ($comments as $comment) {
$output .= "\n<li>".strip_tags($comment->comment_author)
.":" . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\" title=\"on " .
$comment->post_title . "\">" . strip_tags($comment->com_excerpt)
."</a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;
echo $output;?>

Thanks to WPHacks for this awesome coding.

How to Display Most Popular Posts Without a WordPress Plugin

Many blogs have display most popular posts on their blog to make visitors stay longer on their posts, this is one of the way to attract more visitors to your blog. We actually can install a wordpress popular posts plugin to display our most popular posts, but if you don’t want to use plugin, you can simply hack your wordpress theme to do it manually.

Copy and paste the following code to the sidebar.php file, if you want to change the number of displayed posts, simply change the “5″ to your desired number.

<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

Please note that the 5 most popular posts displayed is basing on the 5 most commented posts .