• Resolved jarljmr

    (@jarljmr)


    Hi Hector. Thank for awesome plugins. I have in use for many year! Thank very much!
    I see your snippet for show views in single.php this thread:
    https://ww.wp.xz.cn/support/topic/show-wpp_get_views-with-shortcode/

    I use:

    /**
     * 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' );

    I want show count views for index.php whit shortcode. But, this code no show views, except in single.php

    How i can do?

    Some way for do show coutviews in frontpage?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @jarljmr,

    If you want to show the views count of your posts on your homepage you don’t really need a shortcode. Since you’re going to be editing your index.php file anyways then you could just use this:

    $views_count = wpp_get_views( get_the_ID() );
    echo ($views_count == 1) ? '1 view' : $views_count . ' views';

    (This need to be placed somewhere inside the loop.)

    Thread Starter jarljmr

    (@jarljmr)

    Thank Hector. I am using Generatepress and add new element > Hook.

    This code:

    <?php
    $views_count = wpp_get_views( get_the_ID() );
    echo ($views_count == 1) ? '1 view' : $views_count . ' views';
    ?>

    Just add ?php and ? for work.

    Thank Hector.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Awesome, glad to know you were able to work it out 🙂

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

The topic ‘Shortcode on index.php’ is closed to new replies.