Title: Show wpp_get_views(). with shortcode
Last modified: December 10, 2018

---

# Show wpp_get_views(). with shortcode

 *  Resolved [itism64](https://wordpress.org/support/users/itism64/)
 * (@itism64)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/)
 * Hi,
 * I am trying to show the total views in the post but can’t get it working with
   shortcode (only from all post [wpp post_html={views}] )
 * What shortcode must i use to show the total views of the post ?
 * Cheers

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-10969080)
 * Hi [@itism64](https://wordpress.org/support/users/itism64/),
 * The [[wpp] shortcode](https://github.com/cabrerahector/wordpress-popular-posts/wiki/1.-Using-WPP-on-posts-&-pages)
   returns a list of all popular posts so that won’t work.
 * Since WordPress Popular Posts doesn’t have a shortcode to display the views count
   of a post or a page you could just simply create a new shortcode, like this for
   example:
 *     ```
       /**
        * Registers the shortcode [wpp_views_count].
        *
        * @author Hector Cabrera (https://cabrerahector.com)
        * @return string
        */
       function wpp_views_count_func() {
           if (
               function_exists('wpp_get_views')
               && is_single()
           ) {
               $views_count = wpp_get_views( get_the_ID() );
               return ($views_count == 1) ? '1 view' : $views_count . ' views';
           }
   
           return '';
       }
       add_shortcode( 'wpp_views_count', 'wpp_views_count_func' );
       ```
   
 * Add that to your theme’s functions.php file and then you can use `[wpp_views_count]`
   on your posts and pages.
 *  Thread Starter [itism64](https://wordpress.org/support/users/itism64/)
 * (@itism64)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-10970575)
 * Hi Hector,
 * Thanks for the quick reply.
 * If i place the code i get a error
 * Your PHP code changes were rolled back due to an error on line 148 of file wp-
   content/themes/astra/functions.php. Please fix and try saving again.
    syntax 
   error, unexpected ‘&’
 * Did i do something wrong here?
 * Cheers
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-10970741)
 * Works for me with the Twenty Seventeen theme so I’m not sure what’s going on.
   Make sure you’re pasting that code snippet at the end of your theme’s functions.
   php file to avoid messing around with the existing code.
 * ![Twenty Seventeen's functions.php file in Theme Editor](https://i0.wp.com/imageshack.
   com/a/img924/9104/C1VPra.jpg?ssl=1)
 *  Thread Starter [itism64](https://wordpress.org/support/users/itism64/)
 * (@itism64)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-10970805)
 * Hola Hector,
 * Changed to Twenty Seventeen and same “error” result
 * Your PHP code changes were rolled back due to an error on line 623 of file wp-
   content/themes/twentyseventeen/functions.php. Please fix and try saving again.
   
   syntax error, unexpected ‘&
 * Cheers
 *  Thread Starter [itism64](https://wordpress.org/support/users/itism64/)
 * (@itism64)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-10970825)
 * Oeps,
 * Seen what i do wrong. The copy /past made small mistake. Works perfect sorry 
   about that.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-10970829)
 * No idea, man. As the image from above clearly shows, copying&pasting that code
   into WordPress’ built-in theme editor works just fine for me.
 * Try changing:
 *     ```
       if (
           function_exists('wpp_get_views')
           && is_single()
       )
       ```
   
 * into:
 * `if ( function_exists('wpp_get_views') && is_single() )`
 * and see if it fixes the issue.
 * **Edit:** saw your last comment just now. Glad to know you were able to work 
   it out!
    -  This reply was modified 7 years, 4 months ago by [Hector Cabrera](https://wordpress.org/support/users/hcabrera/).
 *  [Furkan Başer](https://wordpress.org/support/users/furkanbaser/)
 * (@furkanbaser)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-11094782)
 * i’am using different theme as like orao . is it work the on it?
 * also is this code work for grid plugins?
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-11095383)
 * Hey [@furkanbaser](https://wordpress.org/support/users/furkanbaser/),
 * Yes, this will work on any theme.
 *  [X-Raym](https://wordpress.org/support/users/x-raym/)
 * (@x-raym)
 * [6 years, 10 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-11615150)
 * [@hcabrera](https://wordpress.org/support/users/hcabrera/) This could definitely
   be integrated within the plugin natively 🙂
 * But the “views” suffix as to be stripped so we can put any words we need, or 
   no words at all. 🙂
    -  This reply was modified 6 years, 10 months ago by [X-Raym](https://wordpress.org/support/users/x-raym/).
    -  This reply was modified 6 years, 10 months ago by [X-Raym](https://wordpress.org/support/users/x-raym/).

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Show wpp_get_views(). with shortcode’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [single](https://wordpress.org/support/topic-tag/single/)
 * [views](https://wordpress.org/support/topic-tag/views/)

 * 9 replies
 * 4 participants
 * Last reply from: [X-Raym](https://wordpress.org/support/users/x-raym/)
 * Last activity: [6 years, 10 months ago](https://wordpress.org/support/topic/show-wpp_get_views-with-shortcode/#post-11615150)
 * Status: resolved