• Resolved itism64

    (@itism64)


    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

    (@hcabrera)

    Hi @itism64,

    The [wpp] shortcode 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

    (@itism64)

    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

    (@hcabrera)

    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

    Thread Starter itism64

    (@itism64)

    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

    (@itism64)

    Oeps,

    Seen what i do wrong. The copy /past made small mistake. Works perfect sorry about that.

    Plugin Author Hector Cabrera

    (@hcabrera)

    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!

    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

    (@hcabrera)

    Hey @furkanbaser,

    Yes, this will work on any theme.

    @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 5 years, 5 months ago by X-Raym.
    • This reply was modified 5 years, 5 months ago by X-Raym.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Show wpp_get_views(). with shortcode’ is closed to new replies.