How to Use Shortcodes to Display the Comment Count in WordPress

Home » Snippets » How to Use Shortcodes to Display the Comment Count in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Do you want to display the number of comments on your WordPress posts or pages in a unique way? Perhaps you want to draw attention to the comment section and encourage more engagement from your readers. One solution is to create and use a shortcode to display the comment count anywhere you want on your site. In this article, we’ll show you how to create and implement a shortcode that displays the comment count for any post or page on your WordPress site.

					function wpturbo_comment_count_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'post_id' => get_the_ID(),
        'label' => __( 'Comments', 'wpturbo' ),
        'singular_label' => __( 'Comment', 'wpturbo' ),
    ), $atts, 'wpturbo_comment_count' );

    $count = get_comments_number( $atts['post_id'] );

    $output = '<span class="wpturbo-comment-count">';
    $output .= ( $count === 1 ) ? $count . ' ' . $atts['singular_label'] : $count . ' ' . $atts['label'];
    $output .= '</span>';

    return $output;
}
add_shortcode( 'wpturbo_comment_count', 'wpturbo_comment_count_shortcode' );
				

In this tutorial, we will be showing you how to display the comment count of a post using a shortcode in WordPress. We will use the wpturbo_comment_count_shortcode() function to create the shortcode and hook it into WordPress using the add_shortcode() function.

First, we define the wpturbo_comment_count_shortcode() function and set the default attributes for the shortcode in the $atts array. In the $atts array, we set the post_id attribute to get_the_ID() to get the ID of the current post. We also set the label attribute to 'Comments' and the singular_label attribute to 'Comment' with default values that can be overridden as needed.

function wpturbo_comment_count_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'post_id' => get_the_ID(),
        'label' => __( 'Comments', 'wpturbo' ),
        'singular_label' => __( 'Comment', 'wpturbo' ),
    ), $atts, 'wpturbo_comment_count' );

Next, we get the comment count for the post using the get_comments_number() function and passing in the post ID from the $atts array.

    $count = get_comments_number( $atts['post_id'] );

Then, we create the HTML markup for the comment count and store it in the $output variable. We wrap the comment count in a span element with a class of wpturbo-comment-count. We also use a ternary operator to check if the comment count is exactly 1 and change the wording accordingly based on whether it is singular or plural.

    $output = '<span class="wpturbo-comment-count">';
    $output .= ( $count === 1 ) ? $count . ' ' . $atts['singular_label'] : $count . ' ' . $atts['label'];
    $output .= '</span>';

Finally, we return the $output variable to display the comment count on the frontend of the site when the shortcode is called.

    return $output;
}
add_shortcode( 'wpturbo_comment_count', 'wpturbo_comment_count_shortcode' );

To use this shortcode on your site, simply add the [wpturbo_comment_count] shortcode wherever you want the comment count to be displayed on your post or page. If you want to display the comment count for a specific post, you can also pass in the post_id attribute with the post ID you want to get the comment count for. For example: [wpturbo_comment_count post_id="123"].

Register an account to save your snippets or go Pro to get more features.