Skip to:
Content
Pages
Categories
Search
Top
Bottom

Show # of Views for a Post

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not out of the box.


    caveman92
    Member

    @caveman92

    Here’s the code I used; It worked somehwat, but was giving me the same view count for each topic post. I got the sense that buddypress topics all have the same post id… that’s probably why it was giving me the same count for all… any thoughts?

    THIS WENT IN FUNCTIONS.PHP
    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 View”;
    }
    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);
    }
    }

    THIS WENT IN THE INDIVIDUAL FORUM-TOPIC FILE
    Note that I put open/close bracket for triangular brackets:

    open bracket
    ?php
    setPostViews(get_the_ID());
    ?
    close bracket

    AND THIS GOES WHERE I WANT THE NUMBER OF VIEWS TO SHOW:
    open bracket
    ?php
    echo getPostViews(get_the_ID());
    ?
    close bracket

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show # of Views for a Post’ is closed to new replies.
Skip to toolbar