Skip to content

2. Template tags

Héctor Cabrera edited this page May 25, 2019 · 9 revisions

Template tags are custom WPP functions that can be integrated anywhere in your theme.

wpp_get_mostpopular()

With the wpp_get_mostpopular() template tag you can embed the most popular posts of your blog into your site's sidebar (or anywhere else) without using a widget.

Warning: other users have reported that using this template tag on PHP widgets such as Linkable Title HTML and PHP widget and others might not render the PHP code correctly, making the wpp_get_mostpopular() template tag fail and return "Sorry, no data so far". I suggest using it directly on your theme's sidebar.php file to avoid issues.

Usage:

If you don't pass any parameters to the function, wpp_get_mostpopular() will list the most viewed posts (up to 10) from the last 24 hours:

<?php
if (function_exists('wpp_get_mostpopular')) {
    wpp_get_mostpopular();
}
?>

Let's say you want to display the 5 most commented posts from the last 7 days. In that case, we'll need to pass some specific parameters to the function:

<?php
if (function_exists('wpp_get_mostpopular')) {
    wpp_get_mostpopular(array(
        'limit' => 5, /* list up to 5 posts */
        'range' => 'last7days',
        'order_by' => 'comments'
    ));
}
?>

For a complete list of supported parameters: wp-admin > Settings > Wordpress Popular Posts > Parameters.

wpp_get_views()

The wpp_get_views() template tag returns the views count of a single post/page.

It accepts three parameters:

  • Post ID (integer, required).
  • Time range (string, optional). If time range isn't provided, wpp_get_views() will retrieve the total amount of views. Otherwise, it'll return the number of views received within the specified time range.
  • Number format (boolean, optional). If number format is set to true (default), wpp_get_views() will format the views count with grouped thousands (eg. 1,233). Otherwise, wpp_get_views() will return the views count unformatted (eg. 1233).

Usage:

<?php
if (function_exists('wpp_get_views')) {
    echo wpp_get_views(get_the_ID()); // get_the_ID() only works when used inside The Loop! (https://codex.wordpress.org/The_Loop)
}
?>
<?php
if (function_exists('wpp_get_views')) {
    echo wpp_get_views(15, 'weekly'); // ... where '15' is the ID of the post / page
}
?>
You can’t perform that action at this time.