Home / social / Get total post retweets
  • How to Get total post retweets

    get-total-post-retweets

    Adding the first block of PHP code to your functions.php and then the second tweetCount function into your single.php will display the number of retweets for your blog post.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function tweetCount($url) {
           $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
           $element = new SimpleXmlElement($content);
           $retweets = $element->story->url_count;
        if($retweets){
               echo $retweets;
           }else{
               echo '0';
           }
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php
               tweetCount( get_permalink($post->post_id) );
    ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_permalink,