wordpress - Fatal error: Cannot use object of type stdClass as array in.. functions.php -
found code online , worked of sudden 1 day "fatal error: cannot use object of type stdclass array in.. functions.php" on line $count = absint( $json[0]->total_count );
function ds_post_like_count( $post_id ) { // check 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, , comments (fastest query) $fql .= "from link_stat url = '" . get_permalink( $post_id ) . "'"; // 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 , 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 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() { // 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(); ?>&appid=xxx_your_facebook_app_id','facebook share', '540', '400');return false;" href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&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?&url=<?php the_permalink(); ?>&text=<?php the_title(); ?>&via=xxx_your_twitter_handle','tweet', '540', '400');return false;" href="https://twitter.com/share?&url=<?php the_permalink(); ?>&text=<?php the_title(); ?>&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 closing tag */ ?>
to use output array, need set second parameter 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 ); } }
you can refer this,
Comments
Post a Comment