Post Views Counter Shortcode

Below, you’ll find a detailed guide on how to add the Post Views Counter Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Post Views Counter Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Post Views Counter Plugin and the shortcodes it provides:

Plugin Icon
Post Views Counter

"Post Views Counter is a powerful WordPress plugin designed to track and display the number of views your posts and pages are getting. Enhance your SEO and content strategy with this essential tool."

★★★★☆ (961) Active Installs: 200000+ Tested with: 6.2.3 PHP Version: 5.4.0
Included Shortcodes:
  • [post-views]

Post Views Counter [post-views] Shortcode

The Post-Views-Counter shortcode is a versatile tool for tracking views. It’s designed to count views for posts, terms, and users. This shortcode identifies the type of object being viewed (post, term, or user) and retrieves its ID. It then combines these attributes and returns the view count.

Shortcode: [post-views]

Parameters

Here is a list of all possible post-views shortcode parameters and attributes:

  • id – Unique identifier for the post, term, or user
  • type – Specifies whether the id relates to a post, term, or user

Examples and Usage

Basic example – A simple usage of the post-views shortcode to display the view count of the current post.

[post-views]

Advanced examples

Displaying the view count of a specific post by referencing its ID.

[post-views id=5]

Using the shortcode to display the view count of a specific term by referencing its term ID. The term’s view count will be displayed.

[post-views id=3 type='term']

Using the shortcode to display the view count of a specific user by referencing the user’s ID. The user’s view count will be displayed.

[post-views id=2 type='user']

PHP Function Code

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

Shortcode line:

add_shortcode( 'post-views', [ $this, 'post_views_shortcode' ] );

Shortcode PHP function:

function post_views_shortcode( $args ) {
		$defaults = [
			'id'	=> get_the_ID(),
			'type'	=> 'post'
		];

		// main item?
		if ( ! in_the_loop() ) {
			// get current object
			$object = get_queried_object();

			// post?
			if ( is_a( $object, 'WP_Post' ) ) {
				$defaults['id'] = $object->ID;
				$defaults['type'] = 'post';
			// term?
			} elseif ( is_a( $object, 'WP_Term' ) ) {
				$defaults['id'] = $object->term_id;
				$defaults['type'] = 'term';
			// user?
			} elseif ( is_a( $object, 'WP_User' ) ) {
				$defaults['id'] = $object->ID;
				$defaults['type'] = 'user';
			}
		}

		// combine attributes
		$args = shortcode_atts( $defaults, $args );

		// default type?
		if ( $args['type'] === 'post' )
			$views = pvc_post_views( $args['id'], false );
		else
			$views = apply_filters( 'pvc_post_views_shortcode', '', $args );

		return $views;
	}

Code file location:

post-views-counter/post-views-counter/includes/class-frontend.php

Conclusion

Now that you’ve learned how to embed the Post Views Counter Plugin shortcode, 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 *