GeneratePress theme: How to display the comments count on posts

There is no option to display the comments count on single posts in the WordPress GeneratePress theme. It's possible display the comments count on posts in GeneratePress using its hook.

How to display the comments count on posts in GeneratePress

You can choose to display or hide post meta data under Appearance > Customize > Layout > Blog in GeneratePress.

WordPress GeneratePress theme: Blog options

You can choose to display post date, post author, post categories, post tags and pots navigation. But there is no option to display comments count.

You can add the following code snippet to theme's function file (functions.php) to display comments count:

// Display the comments count in single posts in the WordPress GeneratePress theme
add_filter( 'generate_post_author', 'change_author_to_comments' );
function change_author_to_comments()
{
if ( is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
        echo '| <span class="comments-link">';
            comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
        echo '</span>';
    }

}

Please create a child theme and put the above code to the child theme's function file. If not, the codes you added will disappear when the theme is updated.

After saving the changes and refreshing the site, you will see the comments count in the single posts.

Displaying the comments count in the WordPress GeneratePress theme

If you use the code snippet above, the author name will not be displayed in single posts. If you want to display the author name along with the comments count, please use the following code snippet instead:

// Display the comments count and the author name in single posts in GeneratePress
add_filter( 'generate_post_author', 'change_author_to_comments' );
function change_author_to_comments()
{
if ( is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
            $author = '| <span class="rpwe-author">' . get_the_author() . '</span> ';
            echo $author;        
            echo '| <span class="comments-link">';
            comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
        echo '</span>';
    }

}

GeneratePress offers various hooks (actions and filters) for users to extend the features.

I recently changed the theme on this blog from the popular magazine theme Newspaper to GeneratePress. I think GeneratePress is also good for blog sites.

See also ...


Leave a Comment