Forum Replies Created
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
Eric R. Stoeckel, Jr.Participant
I'm using that code on a new test install (no plugins), and the metro child theme. I am only getting 1 page view per visit.
Maybe a caching plugin or some other plugin is calling the page or code twice for some reason.
Twitter: @estoeckel
Eric R. Stoeckel, Jr.ParticipantIs this what you are looking for?
I got code from 2 different places, marked in a comment above each section:
/** Post Views */ /** -- http://www.wpbeginner.com/wp-tutorials/how-to-track-popular-posts-by-views-in-wordpress-without-a-plugin/ -- */ function wpb_set_post_views($postID) { $count_key = 'wpb_post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } //To keep the count accurate, lets get rid of prefetching remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); function wpb_track_post_views ($post_id) { if ( !is_single() ) return; if ( empty ( $post_id) ) { global $post; $post_id = $post->ID; } wpb_set_post_views($post_id); } add_action( 'wp_head', 'wpb_track_post_views'); function wpb_get_post_views($postID){ $count_key = 'wpb_post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 View"; } return $count.' Views'; } /** Customize the post info function */ /** -- http://my.studiopress.com/snippets/post-info/ -- */ add_filter( 'genesis_post_info', 'post_info_filter' ); function post_info_filter($post_info) { if ( !is_page() ) { $post_info = '[post_date] by [post_author_posts_link] [post_comments] ' . wpb_get_post_views(get_the_ID()) . ' [post_edit] '; return $post_info; }}
Sorry for formatting.
Twitter: @estoeckel
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)