3

I have to display the total number of social shares, like mashable does in it´s website.

It´s not a problem, I found a php code that does the job.

function getTotalShares($atts) {
extract(shortcode_atts(array(
    'cache' => '3600',
    'url' => 0,
    'f' => 0,
    'bgcolor' => '#ffffff',
    'bordercolor' => '#ffffff',
    'borderwidth' => '0',
    'bordertype' => 'solid',
    'fontcolor' => '#7fc04c',
    'fontsize' => '55',
    'fontweight' => 'normal',
    'padding' => '1'
  ), $atts));

 $shareHash = "$cache.$url.$f.$bgcolor.$bordercolor.$borderwidth.$bordertype.$fontcolor.$fontsize.$fontweight.$padding";
 $totalShareRecord = 'totalshares_' . $shareHash;
 $cachedposts = get_transient($totalShareRecord);
 if ($cachedposts !== false) {
 return $cachedposts;

 } else {

   if (!$url) $url = get_permalink($post->ID);

   $json = file_get_contents("http://api.sharedcount.com/?url=" . rawurlencode($url));
    $counts = json_decode($json, true);
    $return = $counts['Twitter'] + $counts['Facebook']['total_count'] + $counts['GooglePlusOne'];
    if ($f) $return = '

' . $return . '

'; set_transient($totalShareRecord, $return, $cache); return $return; } } add_shortcode('totalshares','getTotalShares');

Now the problematic!

The code above get the numbers of shares based on the permalink, which is pulled by get_permalink($post->ID);. My website works as a content aggregator, so I want to pull the number of shares of the original post url, which in my website is filled in the post meta links_link_custom. My question is: Is this possible?

I hope this text is not confusing, please comment if more information is needed.

2
  • Well, it is (a bit) confusing. Wouldn't you just have to replace if (!$url) $url = get_permalink($post->ID); with if (!$url) $url = get_post_meta($post->ID, 'links_link_custom', true); and that's it?
    – tfrommen
    Feb 5, 2014 at 17:35
  • Where did you get the PHP code from and do you have an example of the result? Feb 6, 2014 at 9:39

1 Answer 1

3

I found the solution:

Replace

if (!$url) $url = get_permalink($post->ID);

with

$post_id = get_the_ID();
$url = get_post_meta("$post_id", 'links_link_custom', true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.