lisaleague
4/6/2015 - 12:14 AM

add post views

add post views

add_shortcode( 'modified_date', 'wpsites_modified_date_shortcode' );

/**LL
 * Generates the modified date of the post.
 * @author    Brad Dalton
 * @example   http://wpsites.net/
 * @copyright 2014 WP Sites*/
 
function wpsites_modified_date_shortcode( $atts ) {

	$defaults = array(
		'after'  => '',
		'before' => '',
		'format' => get_option( 'date_format' ),
		'label'  => '',
	);

	$atts = shortcode_atts( $defaults, $atts, 'modified_date' );

	$display = ( 'relative' === $atts['format'] ) ? genesis_human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'genesis' ) : get_the_modified_date( $atts['format'] );

	if ( genesis_html5() )
		$output = sprintf( '<time %s>', genesis_attr( 'entry-time' ) ) . $atts['before'] . $atts['label'] . $display . $atts['after'] . '</time>';
	else
		$output = sprintf( '<span class="date published time" title="%5$s">%1$s%3$s%4$s%2$s</span> ', $atts['before'], $atts['after'], $atts['label'], $display, get_the_modified_date( $d ) );

	return apply_filters( 'genesis_post_date_shortcode', $output, $atts );

}

//* Get post view counts
function getPostViews($postID){
    $count_key = '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 Views";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = '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);
    }
}

//* Prevent prefetching from adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 

add_action( 'genesis_loop', 'nabm_get_individual_post_count');
function nabm_get_individual_post_count() {
	if ( is_single() ) {
	setPostViews(get_the_ID());
	}
}

//* Add shortcode to display counts in post meta
function nabm_post_views_shortcode() {
	echo getPostViews(get_the_ID());
}
add_shortcode( 'post_views', 'nabm_post_views_shortcode' );

//*LL-Customize the entry meta in the entry header with date modified
add_filter( 'genesis_post_info', 'add_modified_date_post_info' );
function add_modified_date_post_info($post_info) {
	$post_info = 'Updated: [modified_date] by [post_author_posts_link] [post_views] [post_edit]';
	return $post_info;
}

//* Remove the entry meta in the entry header (requires HTML5 theme support)
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );


//*LL-Remove post meta from archive pages
remove_action('genesis_entry_footer', 'genesis_post_meta', 12);
remove_action('genesis_entry_header', 'genesis_post_info', 12);

add_action('template_redirect', 'child_conditional_actions');
function child_conditional_actions() { if( is_single() ) {
add_action('genesis_entry_header', 'genesis_post_info', 12);
}}


//* Customize entry meta in entry header for all Posts that belong to a particular category
add_filter( 'genesis_post_info', 'sk_post_info_filter' );
function sk_post_info_filter($post_info) {
	if ( ! in_category( 'ncidq-reference-books' ) ) {
		return $post_info;
	}

	$post_info = '[post_edit]';

	return $post_info;
}