Facebook Comment Count Shortcode

If you’re using Facebook Comments instead of the standard WordPress comments, the comment count functions in WP will return 0 for all posts. I recommend installing EA Share Count, which gathers social share data (including Facebook Comments), then adding the following code to your theme or core functionality plugin. You can then display the comment count with [fb_comments].

If EA Share Count is not installed, it falls back to the Genesis comment count shortcode, and if you’re not using Genesis it uses the standard WordPress comment count.

/**
 * FB Comments Shortcode
 *
 */
function be_facebook_comments_shortcode() {

	$output = '';

	// Requires EA Share Count, fallback to Genesis shortcode, then to WP comment count
	if( function_exists( 'ea_share' ) ) {

		$count = ea_share()->core->count( get_the_ID(), 'facebook_comments' );
		$text = $count > 0 ? $count . _n( ' Comment', ' Comments', $count ) : 'Leave a Comment';
		$output = '<a href="' . get_comments_link( get_the_ID() ) . '" class="entry-comments-link">' . $text . '</a>';
		
	} else {

		// Genesis comment shortcode
		if( function_exists( 'genesis_post_comments_shortcode' ) ) {
			$output = genesis_post_comments_shortcode();
			
		// Standard WP comment count
		} else {
			$count = get_comments_number();
			$text = $count > 0 ? $count . _n( ' Comment', ' Comments', $count ) : 'Leave a Comment';
			$output = '<a href="' . get_comments_link( get_the_ID() ) . '" class="entry-comments-link">' . $text . '</a>';
		}

	}
		return $output;
}
add_shortcode( 'fb_comments', 'be_facebook_comments_shortcode' );

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk