Last active
June 9, 2017 09:09
-
-
Save topleague/d74fc1653ae9535280d566c6bca911ba to your computer and use it in GitHub Desktop.
Add Post Views on Genesis Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add Post Views on Genesis Framework | Credit: http://www.joemaraparecio.com/how-to-add-post-views-in-genesis/ | |
// Set Post Views :It counts everytime single posts is viewed | |
if ( !function_exists( 'ja_setPostViews' ) ){ | |
function ja_setPostViews( $postID ){ | |
$count_key = 'ja_post_views'; | |
$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 ); | |
} | |
} | |
} | |
// Get the number of views | |
if ( !function_exists( 'ja_getPostViews' ) ){ | |
function ja_getPostViews( $postID ){ | |
$count_key = 'ja_post_views'; | |
$count = get_post_meta( $postID, $count_key, true ); | |
if( $count == '0' || $count == '' ){ | |
delete_post_meta( $postID, $count_key ); | |
add_post_meta( $postID, $count_key, '0' ); | |
$label = ''; | |
return; | |
}else{ | |
$label = ( $count == '1' ) ? __( '', 'text-domain' ) : __( '', 'text-domain' ); | |
} | |
return $count.$label; | |
} | |
} | |
// Set the post view code in single page | |
add_action( 'genesis_before_loop', 'ja_SinglePostView' ); | |
function ja_SinglePostView(){ | |
if( is_single() ){ | |
ja_setPostViews( get_the_ID() ); | |
} | |
} | |
// Post View Shortcode | |
if ( !function_exists( 'ja_PostView_shortcode' ) ){ | |
function ja_PostView_shortcode( $atts, $content = null ){ | |
extract( shortcode_atts( array(), $atts, 'postview' )); | |
return '<span class="ja_post_view" >'.ja_getPostViews( get_the_ID() ).'</span>'; | |
} | |
add_shortcode( 'postview', 'ja_PostView_shortcode' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment