Snippets: 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 = '<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 24 24" width="1em" fill="currentColor" class="wpex-mr-5"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M17 4h3v16h-3V4zM5 14h3v6H5v-6zm6-5h3v11h-3V9z"/></svg>';
		$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( 'wpex_blog_entry_meta_sections', 'myprefix_custom_meta_sections' );
add_filter( 'wpex_blog_single_meta_sections', 'myprefix_custom_meta_sections' );

Snippet Type: PHP

All PHP snippets should be added via a child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)