Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter lokide

    (@lokide)

    Nevermind, I figure it out! ??

    Thread Starter lokide

    (@lokide)

    Since you were so kind to reply here, I want to ask you something.

    I have a news website, so I’ve added a counter for the posts.

    Add this codes from the following block in your themes function.php file. It will configure your theme to enhance this functionality:

    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);
    }
    }
    // Remove issues with prefetching adding extra views
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

    Now add the following line of code in your single.php file within the loop. It will track the views and set the views of each post.

    setPostViews(get_the_ID());

    And then I’ve edited the skin-base.php in a way to show the counter just for logged it users with this code:

    <span class="sobellosofigo">- <?php echo getPostViews(get_the_ID());?></span>

    <style>
    .sobellosofigo {
    display: none;
    }

    .logged-in .sobellosofigo {
    display: inline;
    }
    </style>

    Everything was working perfectly. THEN I found the Theme Builder Single function, and I needed it, so I’ve started using it and now the counter is not working anymore. I suppose because I have no idea now where to add the single.php part of the code for the loop, or well, I’ve tried a few solution, but instead of showing the post counter, it’s showing me for every post the counter for the entire website, if I refreh the homwpage, it will increase by 1 on every posts.

    Any idea? Can you help me somehow? Where is the loop in elementor? The one like the “single.php” of the theme!

    Thank you and regards,
    Stefano.

Viewing 2 replies - 1 through 2 (of 2 total)