Skip to content

Add Post Views Counter to Post Meta Builder Module

Important

Custom code no longer required since version 6.8 the option is available if you have the Post Views plugin active.

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 element.
add_filter( 'vcex_post_meta_section_choices', function( $sections ) {
	$sections['post_views'] = esc_html__( 'Post Views', 'total-theme-core' );
	return $sections;
} );

// Add post views output for the post meta element.
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>' . esc_html( $views ) . '</li>';
	}
}, 10, 3 );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Related Snippets
Back To Top