Skip to content

Instantly share code, notes, and snippets.

@jeherve
Last active May 5, 2024 02:49
Show Gist options
  • Save jeherve/6328c232f91977a6924805d93490c152 to your computer and use it in GitHub Desktop.
Save jeherve/6328c232f91977a6924805d93490c152 to your computer and use it in GitHub Desktop.
[Post Views For Jetpack] Display a Stats counter at the top of every post, with a "Views:" heading.
<?php
/**
* Display a Stats counter at the top of every post, with a "Views:" heading.
* @see https://wordpress.org/support/topic/page-views-off/
*
* @param string $content Post content.
*/
function jeherve_custom_display_post_views( $content ) {
$post_views = sprintf(
'<div class="stats_counter sd-content"><h3 class="sd-title">%1$s</h3><div class="sd-content">%2$s</div></div>',
esc_html__( 'Views:', 'jetpack' ),
esc_html( do_shortcode( '[jp_post_view]') )
);
/*
* Add to the bottom of each single post
* but not on pages or on the homepage.
*/
if ( is_singular( 'post' ) ) {
return $content . $post_views;
}
return $content;
}
add_filter( 'the_content', 'jeherve_custom_display_post_views' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment