Skip to content

Instantly share code, notes, and snippets.

@topleague
Last active Jun 9, 2017
Embed
What would you like to do?
Add Post Views on Genesis Framework
// 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