Skip to content

Snippet: Add Post Views to the Archive & Post Meta

The following includes 2 examples showing how to add the Post Views Counter plugin views to your blog post entry and single post meta sections. The first example shows how to use a theme icon "eye" and the second is showing how to use a custom SVG. Make sure to only include one of the "myprefix_custom_meta_sections" functions and not both and to include the hooks no matter which function you choose to use.

// Example 1: Using a theme icon.
function myprefix_custom_meta_sections( $sections ) {
	if ( ! function_exists( 'pvc_get_post_views' ) ) {
		return $sections;
	}
	$sections['new_section'] = function() {
		$icon = wpex_get_theme_icon_html( 'eye', 'wpex-mr-10' );
		$views = pvc_get_post_views( get_the_ID() );
		$views = ( $views == 1 ) ? '1 View' : intval( $views ) . ' Views';
		echo  $icon . $views;
	};
	return $sections;
}

// Example 2: Using a custom SVG icon.
function myprefix_custom_meta_sections( $sections ) {
	if ( ! function_exists( 'pvc_get_post_views' ) ) {
		return $sections;
	}
	$sections['new_section'] = function() {
		$icon = '';
		$views = pvc_get_post_views( get_the_ID() );
		$views = ( $views == 1 ) ? '1 View' : intval( $views ) . ' Views';
		echo '<span class="wpex-inline-flex wpex-items-center">' . $icon . $views . '<span>';
	};
	return $sections;
}

// Hooks: Add the new meta section to the blog.
add_filter( 'totaltheme/blog/meta_blocks/entry_blocks', 'myprefix_custom_meta_sections' );
add_filter( 'totaltheme/blog/meta_blocks/singular_blocks', 'myprefix_custom_meta_sections' );
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)
Back To Top