Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    381
    Thanks
    150
    Thanked 2 Times in 2 Posts

    Wordpress not summing well FB and WP comments

    Hi, I am using the following code:

    PHP Code:
    <a href="<?php comments_link(); ?>"><?php 
        $commentCount 
    full_comment_count();
        if ( 
    $commentCount == ) {
            echo 
    'add a comment';  
        } 
        else if ( 
    $commentCount == ) {
            echo 
    'comment';
        }
        else {
            echo 
    '' $commentCount ' comments';    
        } 
    ?></a>
    and this code:
    PHP Code:
    // Get combined FB and WordPress comment count
    function full_comment_count() {
    global 
    $post;
    $url get_permalink($post->ID);

    $filecontent file_get_contents('https://graph.facebook.com/?ids=' $url);
    $json json_decode($filecontent);
    $count $json->$url->comments;
    $wpCount get_comments_number();
    $realCount $count $wpCount;
    if (
    $realCount == || !isset($realCount)) {
        
    $realCount 0;
    }
    return 
    $realCount;

    Problem explained:
    If there are 2 FB comments and 3 WP the output is: 5 comments
    If there are no comments the output is:add a comment
    If there are 0 FB comments and 1 WP the output is: comment

    In the last example is missing the number 1.
    Anybody can find the error in the code?

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    <a href="<?php comments_link(); ?>"><?php 
        $commentCount 
    full_comment_count();
        if ( 
    $commentCount == ) {
            echo 
    'add a comment';  
        } 
        else if ( 
    $commentCount == ) {
            echo 
    '1 comment';
        }
        else {
            echo 
    '' $commentCount ' comments';    
        } 
    ?></a>
    or...

    PHP Code:
    <a href="<?php comments_link(); ?>"><?php 
        $commentCount 
    full_comment_count();
        if (!
    $commentCount) {
            echo 
    'add a comment';  
        } elseif (
    $commentCount == 1) {
            echo 
    '1 comment';
        } else {
            echo 
    $commentCount ' comments';    
        } 
    ?></a>
    or ...

    PHP Code:
    <a href="<?php comments_link(); ?>"><?php 
        $commentCount 
    full_comment_count();
        if (
    $commentCount) {
            echo 
    $commentCount ' comment' . ($commentCount 's' '');
        else {
            echo 
    'add a comment';  
        } 
    ?></a>
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    utnalove (08-18-2012)

  • #3
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    381
    Thanks
    150
    Thanked 2 Times in 2 Posts
    wow 3 solutions! Thanks so much!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •