How can comments from your other posts help you get more page views

Whether you like them or not, comments are important to almost every blog out there. If you’re writing about something, showing pictures, videos or anything else, you probably want people to like your posts and comment on them. Also, comments may single out a post which people actually find the most interesting so you can make that post sticky, place it on your slider or simply take extra attention to it.

While you can see comment count in each post, sometimes you will need to show a number of comments for a specific post somewhere else. Instead of showing a list of most commented posts you can create a shortcode which will put a number of comments with a link to the post you have chosen.

That shortcode can be used anywhere on your site and you can easily manipulate it to show any posts’ comment count.

Alright, let’s build a shortcode:

  1. Open functions.php file
  2. Copy and paste the following code:
  3. function comments_shortcode($atts) {
    extract( shortcode_atts( array(
    'id' => ''
    ), $atts ) );
     
    $num = 0;
    $post_id = $id;
    $queried_post = get_post($post_id);
    $cc = $queried_post->comment_count;
    if( $cc == $num || $cc > 1 ) : $cc = $cc.' Comments';
    else : $cc = $cc.' Comment';
    endif;
    $permalink = get_permalink($post_id);
    return '<a href="'. $permalink . '" class="comments_link">' . $cc . '</a>';
    }
    add_shortcode('comments', 'comments_shortcode');
    
  4. Save changes
  5. Use the following shortcode in order to show comment count for a post:
  6. [comments id=”1″]

Now that you have created a shortcode, you only have to choose where you want to show it and which post you want to analyze. You can place the shortcode in post content, in your sidebar, header or footer or actually wherever you need to show the comment count for that specific post.

Don’t forget to change the ID to an actual post ID you want to track.

Style the comment count:

If you want to style your newly created comment count/link, you can do that with some fancy CSS code. As you may have noticed from the function, your shortcode has been given a class. Simply target the “comments_link” class and style it whatever you want to. Here’s an example.

.comments_link {
color: green;
font-size: 20px;
}

That’s it. The number of posts will be displayed in the location you have chosen, and automatic link to that post will be created. How awesome is that?

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “How can comments from your other posts help you get more page views

  1. […] If you want more comments on your posts, you should think about getting a better WordPress hosting. When you are handling a larger number of comments, it can sometimes slow down your website if your hosting can’t step up to the challenge. Make sure your hosting can handle it when your popularity is growing. […]

Leave a Reply

Your email address will not be published. Required fields are marked *