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?
- Function to execute when a post is moved to trash .
- Count posts within a custom post type and specific taxonomy and terms?
- How set featured posts using checkbox in post edit screen?
- The hook for the AJAX Add to Cart button?
- WP_Query: Why is sticky post not first item in loop?
- Display content from a specific category
- how to display full post with pagination on home page
- This AJAX Code Doesn’t Work – Looking for elegant solution
- WordPress Delete hook with wp_delete_post function?
- How to use alternate post layout at key points in post loop?
- Showing random content / pictures from earlier posts in a sticky post?
- Count to how many categories a post belongs to
- How to run a function when post is edited or updated using publish post action?
- Different style for most recent article
- Differentiate between posts and pages in search results
- Create cron job without a plugin?
- Loop through all product posts?
- Count search results in heading
- How to hide first gallery for every post?
- Display post category in foreach loop with category link
- Displaying Latest Posts on a Page
- Display all posts that were published before full post on single.php
- Two loops, one AJAX loop, exclude posts in first loop from second loop, loops are in different files
- Hide a specific category in admin All Posts page (WordPress)
- Updating post data on save (save_post vs wp_insert_post_data)
- List authors with the last post title and order by last post date
- Hooking into the post editing screen for an existing page only
- How to make the first post in the loop be styled like a “new / featured” post?
- “Right Way” to make custom Loop respect reading settings inside admin panel?
- Only display sticky post or latest post in custom loop
- Display Notification Bar on Header on Certain Post Count
- How to make a sum of all posts displayed in a loop
- How to check if single.php has already called the_post_thumbnail function
- why does wordpress ignore the post args?
- First x post with another template then the others
- Loop doesn’t show title of second post and posts thereafter
- Grid post page on WordPress with Bootstrap, the_excerpt(); Problem
- Loop parent terms {display posts} AND loop child terms {display posts}
- add bootstrap post slider with tabs
- How to add add more properties to WP_Post object in search results loop
- Add Blog to WP homepage Manually
- How can I remove the first two words and shorten get_the_title()
- How to show all posts by author on buddypress profile with navigation
- How to add automatically bootstrap 4 order-lg-1 and order-lg-2 classes for columns in foreach loop based on the count?
- Is it possible to add a shortcode below post title?
- Querying another post category to match current post and display in loop
- separate the post of a category by subcategory
- Optimize CPT-function with a loop
- Display amount of pending posts in back/front end
- Loop not showing first post
- Limiting the amount of posts retrieved by the loop
- Custom Post Type Single Loop Outputting Wrong Post
- Error while excluding a post from another loop (using its ID)
- Run a function when a custom post is update?
- Multiple post in One Loop in wordpress
- How to distinguish pages created by woocommerce?
- How to prevent post update using transition_post_status
- Counting Posts by Category
- Show post excerpt
- Show author bio box
- Use special template for the first post in the loop
- Can the_post_navigation() be outside of the loop?
- WordPress infinite post cycle problem
- Can’t get post_updated hook to work
- Is it possible to completely replace a post with an action/filter?
- List of authors posts minus very latest
- Querying multiple categories in the WP loop if one of the category IDs is dynamically obtained
- How to remove related post from home page
- show latest authors blog post
- Checking if some condition is matched before inserting post into database
- fire action when post is being edited as well as saved?
- Is there a way to separate wordpress titles from their posts?
- adding custom meta as well as looping through posts
- Redirecting posts in WordPress to a specific page
- post_count not working
- When and how will php code in a user made WordPress page be executed?
- Query wordpress posts on static page
- 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