Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show tweet count as text for posts WordPress

  • SOLVED

I want to show my facebook share and retweet counts (and links) as text links only to help with page load speed. The facebook is working, but I can't get the twitter one to work.

Here is the code that is supposed to do the trick:

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){ return $retweets; } else { return 0; } }

Which is called by $rt = tweetCount(get_permalink()); echo

Only I need it to be able to show top and bottom of posts (see here for post example - http://dropdeadgorgeousdaily.com/2011/06/dshop-daily-love-cross-my-heart/) as well as on each post on the homepage and archive pages, and it won't let me call the funciton more than once. The facebook one is a different set up, which I have tried to replicate but without any luck.

Here is the working facebook code for reference:

<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$fbLinkStats = simplexml_load_file('http://api.facebook.com/restserver.php?method=links.getStats&urls='.$shareUrl);
?>

<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>

<?php
if($fbLinkStats->link_stat->total_count == 0) {
echo 'Share it!';
} elseif($fbLinkStats->link_stat->total_count == 1) {
echo 'One share';
} else {
echo $fbLinkStats->link_stat->total_count.' shares';
}
?>
</a>


Another example of a site that has this working is here - http://www.mamamia.com.au/

Thanks in advance for you help!

Answers (1)

2011-06-07

Baki Goxhaj answers:

Here goes the working Twitter code:

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)
return $retweets;
else
return 0;
}

// Used for testing
//$rt = tweetCount('http://proverbhunter.com/a-good-husband-makes-a-good-wife/');

$rt = tweetCount(get_permalink());

echo $rt;


kateM82 comments:

Sorry to ask a dumb question, but could you please make it thesis friendly and wrap it up in the <php ... bits where needed (like the facebook example) That always confuses me.


Baki Goxhaj comments:

Here you go:

This is the functions - it goes in a functions.php file normally and is not repeated in different files.

<?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)

return $retweets;

else
return 0;

}
?>


Put this where you need to show the count. It goes inside the WordPress loop. Show time:

<?php echo tweetCount(get_permalink()); ?>


kateM82 comments:

It worked, hurray, thanks so much!


Baki Goxhaj comments:

Great. Happy to hear that. Please assign the price money to my username. Thanks.


kateM82 comments:

Sorry spoke to soon, it was working, but now I am getting this error?


Warning: file_get_contents(http://api.tweetmeme.com/url_info?url=http://dropdeadgorgeousdaily.com/2011/06/if-you-only-buy-three-things-this-month-june-2011/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php on line 390

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php:391 Stack trace: #0 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php(391): SimpleXMLElement->__construct('') #1 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php(431): tweetCount('http://dropdead...') #2 [internal function]: face_book_likeHP(1) #3 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-includes/plugin.php(395): call_user_func_array('face_book_likeH...', Array) #4 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/lib/html/hooks.php(107): do_action('thesis_hook_aft...', 1) #5 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/lib/functions/content.php(211): thesis_hook_after_post(1) #6 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-co in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php on line 391
Fatal Error (show)



Very weird as it was working?