0

I just need to add views count next to the post link in top-post.php. Not sure how to add these. Can anyone assist?:

<div class="widgets-list-layout-links">
        <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp">
        <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
    </a>
</div>
0

1 Answer 1

0

Use this code in child theme or custom snipets plugin:

/**
 * Top Posts Widget: add post views below each post.
 */
function jetpackme_add_views_top_posts( $post_id ) {
if( function_exists( 'stats_get_csv' ) ) {
     $stats_query = "page_stats_for_" . $post_id;

     if ( false === ( $special_query_results = get_transient( $stats_query ) ) ) {
       $stat_options = "post_id=".$post_id."&days=-1";
       $post_stats = stats_get_csv( 'postviews', $stat_options );
       $special_query_results = $post_stats[0]["views"];
       set_transient( $stats_query, $special_query_results, 6 * HOUR_IN_SECONDS );
     }

     if (($special_query_results != null) && ($special_query_results > 10))
     {
       echo "<div class='top_posts_views'>" . number_format($special_query_results) . " прочита</div>";
     }
}
}

add_action( 'jetpack_widget_top_posts_after_post', 'jetpackme_add_views_top_posts' );

based on ths article: https://shkspr.mobi/blog/2016/04/displaying-wordpress-posts-jetpack-statistics-using-stats_get_csv/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.