Snippets: Add Post Views Counter to Post Meta Builder Module

If you are using the Post Views Counter plugin and wish to display post views using the Total Post Meta module you can do this easily with a little custom code.

// Add new section option for Post Meta module
add_filter( 'vcex_post_meta_sections', function( $sections ) {
	$sections[ esc_html__( 'Post Views', 'total-theme-core' ) ] = 'post_views';
	return $sections;
} );

// Add post views output for the post meta module
add_filter( 'vcex_post_meta_custom_section_output', function( $type, $icon_class ) {
	if ( 'post_views' == $type && function_exists( 'pvc_get_post_views' ) ) {
		global $post;
		$views = pvc_get_post_views( $post->ID );
		$views = ( $views == 1 ) ? '1 View' : intval( $views ) . ' Views';
		return '<li><span class="' . esc_attr( $icon_class ) . '"></span>' . $views . '</li>';
	}
}, 10, 3 );

Result:

All PHP snippets should be added via a child theme's functions.php file.