You could hook into template_redirect
and execute a helper function:
add_action( 'template_redirect', 'wpse_75558_count' );
function wpse_75558_count()
{
if ( is_singular() )
setPostViews( get_the_ID() );
}
To display the post views, you have to use a later hook. I would recommend the_content
:
add_filter( 'the_content', 'wpse_75558_show_count' );
function wpse_75558_show_count( $content )
{
$count = getPostViews( get_the_ID() );
if ( ! is_numeric( $count ) )
$count=""; // prevent errors showing to visitors
else
$count = "<p class="view-count">Views: $count</p>";
return $content . $count;
}
Just an idea, I haven’t tested the code.
Related Posts:
- Count singular post views automatically
- Action hook ‘wp’ firing twice… why?
- Alternate custom content in the loop
- Custom function for “Submit for Review” hook
- How can I hook into creating a new post and execute wp_die(), before the post is inserted into the database?
- Double count view in archive.php
- How can i do something after head like adding a hook for after head but before post
- Adding buttons to Add New Post and Add New Page
- How to access $post from a callback function
- Copy post to separate database with “add_action(….)”
- Call Web Services on post first publish
- What does WordPress do if I save a post without content/title? [duplicate]
- Divs appearing everywhere in post content
- Automatically set post_parent value
- Is there a way to know when a page has been updated and do some action only once?
- Is update_post_meta used when save_post action hook is invoked?
- Retrieve posts inside foundation tabs and tab-content split by 3 per row
- Alternative content between posts no repetition
- Grab meta data before post is saved
- How to control Post view count incresing in sidebar widget posts too?
- How to get total posts count for each date?
- Modify upload directory to use post category slug in file path on multisite installation
- Comment count next to post title?
- First x post with another template then the others
- Loop doesn’t show title of second post and posts thereafter
- Change all author links in Blog roll
- Grid post page on WordPress with Bootstrap, the_excerpt(); Problem
- Showing categories and subcategories with posts
- Second get_permalink inside loop points to wrong URL
- Get the amount of posts on a current page
- Howto show comment count by post in post?
- Loop parent terms {display posts} AND loop child terms {display posts}
- How to add a class to each individual post?
- Adding bootstrap classes to video shortcodes
- add bootstrap post slider with tabs
- Counter is skipping post when I still want it visible [closed]
- How to add add more properties to WP_Post object in search results loop
- Custom taxonomy archive page problem
- preg_replace not removed “class”
- Loop don’t work within single.php page
- Add a minimum word count to Excerpt (and how to force Excerpt as a rule for WyPiekacz)
- Display Post by menu order
- Make assigning post to a specific category equivalent to assigning it to all categories
- How to inject a post within a loop
- wordpress taxonomy results
- Getting rid of the blog page entirely
- Which hooks are essential for post templates?
- Displaying posts on Homepage
- Where are my posts linked to? I keep getting a Not Found Error, and I have no single.php page
- Sort by category and then date?
- Only show posts with image and the correct ammount of posts in loop
- Is there a way to separate wordpress titles from their posts?
- Related posts by searching post tags of single post as terms
- The loop: more posts than I want to fit on one page
- adding custom meta as well as looping through posts
- Loop not allowing infinite scroll after post per page limit
- Customise the appearance of 4 specific post ID’s within the loop?
- How to limit the number of posts a user can publish based on user type
- what template-part to call, to have my post in the center of the page?
- Redirecting posts in WordPress to a specific page
- WordPress posts loop pagination – first page return 125 posts instead of 10 and the rest return 10
- ho can I get the number of the current post instead of the post id?
- The post order is different for logged-in and non-logged-in users? [closed]
- Index.php is ordering posts strangely
- How to show only the date, the title and a little “summary” of my WordPress post in my custom theme?
- save_post not working
- Show all posts in category
- ‘posts_per_page’ => ’10’ does not show any post
- How do I show the post title if an advanced custom field hasn’t been used?
- post_count not working
- First post in loop displays twice
- Wrap every 2 posts
- Adding Count Post in Page
- How do I create a template page to show 3 blog posts?
- Action while post is being published
- When and how will php code in a user made WordPress page be executed?
- Query wordpress posts on static page
- posts_per_page – Repeats only first posts ‘post__in’ array
- Custom post order returning posts from other categories
- Do anything on post_status change [duplicate]
- blog post displaying within older post – loop issue?
- CSS code for “inserting a custom post divider” after each post
- What function does the loop of displaying posts?
- What template file is used by default for posts?
- Count words for all posts by all authors
- Word count for all posts of all authors
- View post with specific category id and name which I selected in the backend (drop-down option)
- Display post count on archive page in reverse order
- WordPress Ajax load doesn’t work
- Admin notice on wp_insert_post
- Count the number of matching post names in foreach loop
- Global $post value outside the loop
- WordPress current post functions inside loop doesn’t work anymore, are they incompatible with php 8.0?
- List of published post dates only outputting one date
- Is it possible to use the_post 2 times in one loop
- Why does ‘get_the_ID’ work outside of the loop?
- WordPress as microblog: show excerpt instead of “(no title)” in posts view
- WordPress Post Query Using Custom native MySQL like St_Distance_Sphere
- Shortcode in loop always display data of first post on the page
- How to add publish button in menu All Posts (Wp-Admin)