• Resolved southcast

    (@southcast)


    I have a following code on my single.php which gives me the total count of the post views. It works just fine.

    <?php echo 'TOTAL POST VIEWS: ' .wpfp_get_current_count(); ?>

    Now, I would like to add 300 views by default. So that if my actual views are 33 then the total post views becomes 333. I guess i need a lesson on some basic php addition. Could somebody please help a little here please. If possible please suggest me a modified code.
    Always thankful to this community.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Well, some people don’t have the chance to respond quite so quickly sometimes, but we try when we can. ??

    Now, your problem is pretty easy really. All you need is something like this:

    <?php echo 'TOTAL POST VIEWS: ' .(wpfp_get_current_count() + 300); ?>

    You may not need the () around the addition part, but I would always use it because it makes any future debugging a whole lot easier.

    Thread Starter southcast

    (@southcast)

    @catacaustic. Thanks for trying but the code did not work. In fact I had tried this earlier. wpfp_get_current_count() still works and displays the actual post views but the addition does not happen. Any suggestion ?

    <?php
    function wpfp_get_current_count () {
        return 40;
    }
    echo 'TOTAL POST VIEWS '.(wpfp_get_current_count () + 300);

    Using that as an example, it displays the count as 340, so I know that code works (I did actually test this one).

    The only reason that I can think of that might do anything differently is if the wpfp_get_current_count() function echos the total and doesn’t return it as it normally should. I’d suggest that you dig around in that function to see what it’s actually doing.

    Thread Starter southcast

    (@southcast)

    Whoops ! This one throws a Fatal error: Cannot redeclare wpfp_get_current_count(). I will paste here what I have in my custom-functions.php. Kindly take a look and suggest me an appropriate modification in the code.

    function wpfp_get_current_count() {
        global $wpdb;
    	$current_post = get_the_ID();
        $query = "SELECT post_id, meta_value, post_status FROM $wpdb->postmeta";
        $query .= " LEFT JOIN $wpdb->posts ON post_id=$wpdb->posts.ID";
        $query .= " WHERE post_status='publish' AND meta_key='wpfp_favorites' AND post_id = '".$current_post."'";
        $results = $wpdb->get_results($query);
        if ($results) {
            foreach ($results as $o):
                echo $o->meta_value;
            endforeach;
        }else {echo( '0' );}
    }

    I really appreciate your help.

    You see where your code says echo? That’s the problem. You need to return that numeric value instead of printing it out. You need to return the value like I did in my example above.

    Thread Starter southcast

    (@southcast)

    Yep, that was the culprit. I recieved the same solution here just a few seconds ago. But I appreciate the effort you took to help. I am marking this resolved. Again, many thanks @catacaustic.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Simple increment in post views.’ is closed to new replies.