0

is there a simple way like a hook or function extension to show the comment count next e.g. behind the post title.

I'd like to show the comment count behind every post shown on my WordPress.

Example: Newspost on Intel (3)

...where (3) is the comment count of the post.

Any hint would be great!

2 Answers 2

0

On most cases you should be able to use the_title filter to modify the title string. Use get_comments_number() to get the comments count for the post - of given ID or the current one.

For example,

add_filter( 'the_title', 'wpse_427277_the_title', 10, 2 );
function wpse_427277_the_title( string $title, $post_id = null ): string {
    $comment_count = (int) get_comments_number( $post_id );
    if ( $comment_count ) {
        $title .= sprintf( ' (%d)', $comment_count );
    }

    return $title;
}

Modify as needed.

1
  • Perfect. That's exactly what i was looking for. It's a small bunch of code but helps a lot. Thank you so much!
    – VSG
    Commented Oct 23 at 15:47
0
    <?php if( comments_open( get_the_ID() ) ) : ?>
                        <a href="<?php the_permalink(); ?>#comments" class="comments-link"><i class="fa fa-comment"></i> <?php echo en2fa( get_comments_number() ); ?></a>
                    <?php endif; ?>
2
  • Thanks a lot for your reply! Would that be possible using a Filter (add_filter) in the functions.php? I mean like overwriting the post_title?
    – VSG
    Commented Oct 17 at 10:16
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 18 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.