2016-08-24 15 views
1

このコードがオンラインで見つかったと突然1日私は "致命的なエラーが発生します:配列としてのタイプstdClassのオブジェクトを使用することはできません。 .function.php "この行に$ count = absint($ json [0] - > total_count);致命的なエラー:array.function.phpの配列としてstdClassオブジェクトを使用することはできません

function ds_post_like_count($post_id) { 

    // Check for transient 
    if (! ($count = get_transient('ds_post_like_count' . $post_id))) { 

    // Setup query arguments based on post permalink 
    $fql = "SELECT url, "; 
    //$fql .= "share_count, "; // total shares 
    //$fql .= "like_count, "; // total likes 
    //$fql .= "comment_count, "; // total comments 
    $fql .= "total_count "; // summed total of shares, likes, and comments (fastest query) 
    $fql .= "FROM link_stat WHERE url = '" . get_permalink($post_id) . "'"; 

    // Do API call 
    $response = wp_remote_retrieve_body(wp_remote_get('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql))); 

    // If error in API call, stop and don't store transient 
    if (is_wp_error($response)) 
     return 'error'; 

    // Decode JSON 
    **$json = json_decode($response);** 

    // Set total count 
    $count = absint($json[0]->total_count); 

    // Set transient to expire every 30 minutes 
    set_transient('ds_post_like_count' . $post_id, absint($count), 30 * MINUTE_IN_SECONDS); 

    } 

return absint($count); 

} /** Facebook End */ 

function ds_social_media_icons() { 



// Get the post ID 
    $post_id = get_the_ID(); ?> 
    <?php print_r($response) ?> 
    <div class="social-icons-wrap"> 
    <ul class="social-icons"> 
     <!-- Facebook Button--> 
     <li class="social-icon facebook"> 
      <a onclick="javascript:popupCenter('https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&amp;appId=XXX_YOUR_FACEBOOK_APP_ID','Facebook Share', '540', '400');return false;" href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&amp;appId=XXX_YOUR_FACEBOOK_APP_ID" target="blank"><i class="fa fa-facebook"></i> Share </a><span class="share-count"><?php echo ds_post_like_count($post_id); ?></span> 
     </li> 
     <!-- Twitter Button --> 
     <li class="social-icon twitter"> 
      <a onclick="javascript:popupCenter('https://twitter.com/share?&amp;url=<?php the_permalink(); ?>&amp;text=<?php the_title(); ?>&amp;via=XXX_YOUR_TWITTER_HANDLE','Tweet', '540', '400');return false;" href="https://twitter.com/share?&amp;url=<?php the_permalink(); ?>&amp;text=<?php the_title(); ?>&amp;via=XXX_YOUR_TWITTER_HANDLE" target="blank"><i class="fa fa-twitter"></i> Tweet </a><span class="share-count"><?php echo ds_post_tweet_count($post_id); ?></span> 
     </li>  
    </ul> 
    </div><!-- .social-icons-wrap --> 

<?php } 
/* DON'T DELETE THIS CLOSING TAG */ ?> 

答えて

0

出力を配列として使用するには、2番目のパラメータをtrueに設定する必要があります。

if(! is_wp_error($response) 
     && isset($response['response']['code'])   
     && 200 === $response['response']['code']) 
    { 
     $body = wp_remote_retrieve_body($response); 
     $fb = json_decode($body); 

     if(! isset($fb->likes) && isset($fb->shares)) 
     { 
      $fb->likes = $fb->shares; 
     } 

     if(isset($fb->likes)) 
     { 
      $myfblikes = sprintf('%04s', (int) $fb->likes); 
      update_post_meta($post->ID, 'fb_likes', $myfblikes); 
     } 
    } 

あなたはthisを参照することができ、

+0

$ JSON = json_decode($応答、真の);これは、検索結果のレンダリングをさせないようになります..いくつかのサイトのソリューションとして見ましたが、何らかの理由でこのためには動作しません。 –

+0

print_r($ response)は何ですか? –

+0

私のPHPの無知を赦し、私は結果をレンダリングする関数phpの領域に "print_r ....."を追加しました。エラーは10行前ですので、 '致命的なエラー:配列stdClassのオブジェクトを配列として使用できません'という場所で停止するため、印刷を追加する場所が最も適切です。私は申し訳ありませんが、私は多くのPHP –

関連する問題