WordPress: visualizzare il numero di retweet per i post
Due tecniche per visualizzare il numero di retweet per i nostri post di WordPress.
Twitter è sempre più integrato nei nostri siti in WordPress. In questo articolo vedremo come visualizzare il numero di retweet per uno specifico post.
Aggiungete il seguente codice nel file functions.php:
|
1 2 3 4 5 6 7 8 9 10 |
function retweets_count($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;
}
} |
Quindi possiamo utilizzare la funzione nel nostro tema usando il link permanente del post corrente:
|
1 2 |
$url = get_permalink();
echo retweets_count($url); |
Questa soluzione sfrutta le API di Tweetmeme. Se volete utilizzare un’altra soluzione, vi consiglio di usare le API di Twitter:
|
1 2 3 4 5 6 7 8 9 10 11 |
function retweets_count($url) {
$content = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($content, true);
$retweets = $json['count'];
return $retweets;
} |
2 commenti
Olá!
Vou perguntar em português / ingles mesmo porque não domino o italiano…
Você sabe se por essa forma os links encurtados também são contados? Porque o próprio twitter encurta e tem também serviços de terceiros para isso..
Obrigado!
——–
Hi!
I’ll ask in english / portuguese because I can’t write in Italian..
Do you know if shortened links are tracked also using your technique? I’m asking because twitter itself shorten our links, and we have third party services..
Thanks!
Yes. I think so, though I didn’t run extensive tests in that vein.
I commenti sono chiusi.