2 out of 2 members found this post helpful.
Numarul de "share-uri" al unui articol pe facebook, twitter si google plus [cod]
Codul de mai jos se include in fisierul single.php (de obicei) si afiseaza de cate ori a fost impartit/"share-uit" un articol pe facebook, twitter si google plus:
Cod:
function social_shares() {
$url = get_permalink( $post_id );
$json = file_get_contents("http://api.sharedcount.com/?url=" .
rawurlencode($url));
$counts = json_decode($json, true);
$totalcounts= $counts["Twitter"] +
$counts["Facebook"]["total_count"] +
$counts["GooglePlusOne"];
echo "<div>$totalcounts Share</div>";
}
Sper sa va fie de folos.
LE: Cine o inventa codurile astea, nu stiu. Codul nu functioneaza, ca mi-e greu sa-l testez inainte... Revin.
Ultima modificare făcută de Bogdan Calin; 8th March 2013 la 19:56.
1 out of 1 members found this post helpful.
E foarte ok codul, tau, dar are un dezavantaj. Apelul catre http://api.sharedcount.com/?url= se face servere side, adica de catre serverul tau, iar la un numar mare de apeluri sau in cazul in care api.sharedcount.com raspunde greu si pagina ta se va incarca greu. Poti face asta cu un script jquery, lasand in seama clientului sa faca apelul.
jQuery.sharedCount = function(url, fn) {
url = encodeURIComponent(url || location.href);
var arg = {
url: "//" + (location.protocol == "https:" ? "sharedcount.appspot" : "api.sharedcount") + ".com/?url=" + url,
cache: true,
dataType: "json"
};
if ('withCredentials' in new XMLHttpRequest) {
arg.success = fn;
}
else {
var cb = "sc_" + url.replace(/\W/g, '');
window[cb] = fn;
arg.jsonpCallback = cb;
arg.dataType += "p";
}
return jQuery.ajax(arg);
};
jQuery(document).ready(function($){
$.sharedCount(location.href, function(data){
$("#tweets").text(data.Twitter);
$("#likes").text(data.Facebook.like_count);
$("#plusones").text(data.GooglePlusOne);
$("#sharedcount").fadeIn();
});
});