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>
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>
Share
Improve this question
edited Apr 30, 2019 at 10:21
bruno
asked Apr 30, 2019 at 9:34
brunobruno
13 bronze badges
0
1 Answer
Reset to default 0Use 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/