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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    382
    Thanks
    150
    Thanked 2 Times in 2 Posts

    How to sum Wordpress and Facebook comments

    Hello, I would like to sum two numbers. Here is the explanation.

    The following code shows the wordpress comments count:
    PHP Code:
    <a href="<?php comments_link(); ?>"><?php comments_number('no comments','one comment','% comments'); ?></a>
    The following code gives the Facebook comments count:
    PHP Code:
    <a href="<?php comments_link(); ?>"><div class="fb-comments-count" style="display: inline" data-href="<?php echo get_permalink($post->ID); ?>">0</div> kom z FB</a>
    Together the output is similar to this:
    Code:
    3 comments + 5 kom z FB
    For reference you can see the site.

    However, I would like this output:
    Code:
    8 comments
    It should add the two numbers and get one. How can I do? What is the right php code?

  • #2
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    382
    Thanks
    150
    Thanked 2 Times in 2 Posts
    I was able to do it with this function I found in internet:

    PHP Code:
    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;

    And I am putting
    PHP Code:
    <?php echo full_comment_count(); ?>
    instead of
    PHP Code:
    <?php comments_number('no comments','one comment','% comments'); ?>
    .

    The previous code could show me the word comment for singular, plural or no comments.
    The full_comment_count doesn't do it.

    How can I do to make the full_comment_count choose ('no comments','one comment','% comments')?

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The facebook count is hard because it's javascripting.

    You found this on the internet?
    http://mikepayne.co/getting-facebook-comment-count

    then try this ... of course I didn't test it, so make safe copies of your files ...
    PHP Code:

    <?php 
    $wp_count
    =comments_number('no comments','one comment','% comments'); 
    $facebook_count=get_fb_comment_count();
       if(
    $wp_count == "one comment"){
       
    $wp_count=1;
       }
       if(
    $wp_count == "no comments" ){
       
    $wp_count=0;
       }
    echo 
    "Total comments: "$wp_count $facebook_count;
    ?>

    The function from Mike Payne's site to go into function.php file:

    Notice I edited it so the count is not text ...

    PHP Code:
    function get_fb_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;
         if (
    $count == || !isset($count)) {
                
    $count=0;
              
    //$count = 'No Comments';
         
    } elseif ( $count == ) {
              
    //$count = '1 Comment';
         
    } else {
              
    //$count .= ' Comments';
         
    }
         echo 
    $count;



    .
    Last edited by mlseim; 08-10-2012 at 07:08 PM.

  • Users who have thanked mlseim for this post:

    utnalove (08-10-2012)

  • #4
    Regular Coder
    Join Date
    Oct 2008
    Location
    Poland
    Posts
    382
    Thanks
    150
    Thanked 2 Times in 2 Posts
    Thank you very much!!! That's it )


  •  

    Posting Permissions

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