WP Post Views – WordPress Post views counter Shortcodes

Below, you’ll find a detailed guide on how to add the Wp Post Views – WordPress Post views counter Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Wp Post Views – WordPress Post views counter Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Wp Post Views – WordPress Post views counter Plugin and the shortcodes it provides:

Plugin Icon
Wp Post Views – WordPress Post views counter

"Wp Post Views is a powerful WordPress plugin that enables you to track and display the number of views for each post. Tailored to enhance audience engagement data."

★★★★✩ (5) Active Installs: 3000+ Tested with: 6.3.2 PHP Version: 5.3
Included Shortcodes:
  • [WPPV-TOTAL-VIEWS]
  • [WPPV-TOTAL-VIEWS-PER-POST-TYPE]

Wp Post Views – WordPress Post views counter [WPPV-TOTAL-VIEWS] Shortcode

The WPPV-TOTAL-VIEWS shortcode is used to display the total views of a specific post in WordPress. It retrieves this data from the ‘entry_views’ meta key.

Shortcode: [WPPV-TOTAL-VIEWS]

Examples and Usage

Basic example – In this example, we are using the shortcode ‘WPPV-TOTAL-VIEWS’ to display the total views of the current post. The total views are stored as post meta data with the key ‘entry_views’.

[WPPV-TOTAL-VIEWS /]

Advanced examples

Even though the given shortcode doesn’t have any parameters, we can still modify our function to accept parameters and use them to modify our output. For example, we can add a parameter to format the output.

First, we need to modify our function:


function wppv_current_post_view_callback($atts = array() , $content = ''){
	$meta_key         = 'entry_views';
	$view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
	$atts = shortcode_atts( array(
		'format' => 'plain',
	), $atts, 'WPPV-TOTAL-VIEWS' );

	if($atts['format'] == 'comma'){
		$view_post_meta = number_format($view_post_meta);
	}
	return $view_post_meta;
}

Now, we can use the ‘format’ parameter in our shortcode to display the total views with commas:

[WPPV-TOTAL-VIEWS format="comma" /]

This will output the total views in a format like 1,000 instead of 1000.

PHP Function Code

In case you have difficulties debugging what causing issues with [WPPV-TOTAL-VIEWS] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'WPPV-TOTAL-VIEWS', 'wppv_current_post_view_callback' );

Shortcode PHP function:

                    function wppv_current_post_view_callback($atts = array() , $content = ''){ 
		$meta_key         = 'entry_views';
		$view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
		return $view_post_meta;
	}
                    

Code file location:

wp-post-views/wp-post-views/includes/shortcodes.php

Wp Post Views – WordPress Post views counter [WPPV-TOTAL-VIEWS-PER-POST-TYPE] Shortcode

The WPPV-TOTAL-VIEWS-PER-POST-TYPE shortcode is designed to display the total number of views for a specific post type in WordPress. This shortcode works by calling the ‘wppv_current_post_view_per_post_type_callback’ function, which retrieves the total views using the ‘get_total_views’ method.

Shortcode: [WPPV-TOTAL-VIEWS-PER-POST-TYPE]

Parameters

Here is a list of all possible WPPV-TOTAL-VIEWS-PER-POST-TYPE shortcode parameters and attributes:

  • post_type – defines the type of post to count views for

Examples and Usage

Basic example – A simple usage of the shortcode to display the total views for posts.

[WPPV-TOTAL-VIEWS-PER-POST-TYPE post_type='post' /]

Advanced examples

Using the shortcode to display the total views for custom post types. In this case, we are looking at a custom post type called ‘product’.

[WPPV-TOTAL-VIEWS-PER-POST-TYPE post_type='product' /]

Another advanced usage might be to use the shortcode within a PHP function. This could be useful if you want to display the total views within a theme file or a custom plugin. Here’s how you might do it:

<?php echo do_shortcode("[WPPV-TOTAL-VIEWS-PER-POST-TYPE post_type='product']"); ?>

Remember, the ‘post_type’ attribute is flexible and can be changed to any type of post you have on your WordPress site, whether it’s a built-in post type like ‘post’ or ‘page’, or a custom post type that you or a plugin has defined.

PHP Function Code

In case you have difficulties debugging what causing issues with [WPPV-TOTAL-VIEWS-PER-POST-TYPE] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE', 'wppv_current_post_view_per_post_type_callback' );

Shortcode PHP function:

                    function wppv_current_post_view_per_post_type_callback($atts = array() , $content = ''){ 
		global $wp_post_views;

		$parsed = wp_parse_args(
			$atts,
			array(
				'post_type' => 'post',
			)
		);
		return $wp_post_views->get_total_views( $parsed['post_type'] );
	}
                    

Code file location:

wp-post-views/wp-post-views/includes/shortcodes.php

Conclusion

Now that you’ve learned how to embed the Wp Post Views – WordPress Post views counter Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *