WP-PostViews Shortcode

Below, you’ll find a detailed guide on how to add the WP-PostViews 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 WP-PostViews Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the WP-PostViews Plugin and the shortcodes it provides:

Plugin Icon
WP-PostViews

"WP-PostViews is a powerful WordPress plugin that allows you to track and display the view count of your posts. This tool is ideal for bloggers and site owners who wish to monitor post popularity."

★★★★✩ (62) Active Installs: 100000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [views]

WP-PostViews [views] Shortcode

The WP-PostViews shortcode ‘views’ retrieves the view count of a specific post. It uses the post ID as an attribute and if no ID is provided, it defaults to the current post. The shortcode fetches the view count from post meta-data and formats it according to the plugin’s settings. It can display the count in a rounded or exact format. Shortcode: [views id=”123″] Replace “123” with your post ID to get its view count. The shortcode is flexible and adaptable to various usage scenarios.

Shortcode: [views]

Parameters

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

  • id – Specifies the unique identifier of the post

Examples and Usage

Basic example – A simple way to display the view count of a specific post using its ID.

[views id=3 /]

Advanced examples – Utilizing the shortcode to display view count but without specifying the post ID. This will default to the current post’s ID.

[views /]

Another advanced example shows how to use the shortcode to display the view count of a post. If the specified ID is not found, it will default to the current post’s ID.

[views id=56 /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'views', 'views_shortcode' );

Shortcode PHP function:

function views_shortcode( $atts ) {
	$attributes = shortcode_atts( array( 'id' => 0 ), $atts );
	$id = (int) $attributes['id'];
	if( $id === 0) {
		$id = get_the_ID();
	}
	$views_options = get_option( 'views_options' );
	$post_views = (int) get_post_meta( $id, 'views', true );
	$output = str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) );

	return apply_filters( 'the_views', $output );
}

Code file location:

wp-postviews/wp-postviews/wp-postviews.php

Conclusion

Now that you’ve learned how to embed the WP-PostViews 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 *