Post Views In Byline

Community Forums Forums Design Tips and Tricks Post Views In Byline

This topic is: not resolved

This topic contains 5 replies, has 3 voices, and was last updated by  Eric R. Stoeckel, Jr. 1 year, 9 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #17315

    buddy_boy8403
    Participant
    Post count: 132

    Does anyone have a good solution for putting the post views in the byline?

    #17513

    Susan
    Moderator
    Post count: 9426

    Have you tried the WP-Post Views plugin?

    http://wordpress.org/extend/plugins/wp-postviews/


    Susan @ Graphically Designing I offer theme customizations I tweet!
    I’ve taken up the challenge! – help me answer some of the unanswered posts

    #17514

    buddy_boy8403
    Participant
    Post count: 132

    I looked at it, but how would I get it in the byline via the functions.php without modifying core code?

    #17533

    Eric R. Stoeckel, Jr.
    Participant
    Post count: 5

    Is this what you are looking for?

    I got code from 2 different places, marked in a comment above each section:

    [php]

    /** 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;
    }}

    [/php]

     

    Sorry for formatting.


    Twitter: @estoeckel

    #17554

    buddy_boy8403
    Participant
    Post count: 132

    dev454 – This is exactly what I’m looking for.  The only problem is it’s counting 2 page views for each 1 visit.

    #17559

    Eric R. Stoeckel, Jr.
    Participant
    Post count: 5

    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

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.