I see you’ve already added a snippet to add the post views to every post on your site. That’s a great start. You can now tweak that snippet a bit to:
- Ensure the views do not appear on the home page.
- Ensure the views only appear on posts (not on pages)
- Add the views to the top of the post, instead of the bottom
Here is the snippet you could use to do just that:
/**
* Display a Stats counter at the top of every post.
* @see https://wordpress.org/support/topic/page-views-off/
*
* @param string $content Post content.
*/
function jeherve_sanchitgogia_add_counter_to_posts( $content ) {
$post_views = sprintf(
'<span class="stats_counter rt-reading-time" style="display:inline">
<span class="rt-time">%1$s</span>
</span>',
esc_html( do_shortcode( '[jp_post_view]') )
);
/*
* Add to the top of each single post
* but not on pages or on the homepage.
*/
if ( is_singular( 'post' ) ) {
return $post_views . $content;
}
// Final fallback.
return $content;
}
add_filter( 'the_content', 'jeherve_sanchitgogia_add_counter_to_posts' );
In addition to this, you could go to Appearance > Customize > CSS to customize things further and ensure that both the reading time and the number of views look well together.
/* Ensure that both the reading time and the stats can fit on one line. */
.single-post span.rt-reading-time {
display: inline !important;
}
/* Add a | before the stats counter */
.single-post .stats_counter:before {
content: ' |';
}
/* Add a bit of margin before the actual post content on single posts. */
.single-post .entry-content p:first-of-type {
margin-top: 2rem;
}
I hope this helps.
Thanks much, Jeremy, this is super helpful. Few things, please:
1/ Can still see the views on the pages – for reference please see this – https://greyhoundresearch.com/how-we-help/ – can you please help remove the views on the pages?
2/ Can still see the views on the homepage – please see https://greyhoundresearch.com – can you please help remove the views on the homepage?
3/ On the posts – can we please move the ‘|’ to post the number of views? essentially between number of views and reading time
4/ On the posts – can we please switch off the views at the bottom of the posts? Cause it’s still visible.
Thanks much for your help. This is a brilliant plugin!
Please avoid request #3, have managed to address it. Thank you.
1, 2, 3, and 4 still exist because you haven’t removed the code snippet you were previously using; you’d need to remove it since you’ve added the snippet I provided you instead.
ah, got it, thank you, Jeremy! Your help is much appreciated.