4

I am using the WP REST API plugin in wordpress and want to loop through my json data using a for each loop, however when I var_dump the variable that the json data is stored in it gives me an error.

"Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in C:\wamp\www\public_html\wp-content\themes\raj\template-wiki.php on line 22"

<?php $json = lusso_posts(); ?>

<?php var_dump($json); ?>

<?php echo ($json->wp-json->posts[1]->ID); ?>

I think the "wp-json" is conflicting with "->" this sign because when I remove the "-" from "wp-post", the error goes away but nothing shows.

CODE FROM LUSSO FUNCTION

` function lusso_posts() {

  // Do we have this information in our transients already?
  $transient = get_transient( 'lusso_posts' );

  // Yep!  Just return it and we're done.
  if( ! empty( $transient ) ) {

    // The function will return here every time after the first time it is   run, until the transient expires.
    return $transient;

  // Nope!  We gotta make a call.
  } else {

    // We got this url from the documentation for the remote API.
    $url = 'http://localhost/database2/wp-json/posts/';

    $body =  wp_remote_retrieve_body(wp_remote_get($url));

    $json = json_decode($body);


    // Call the API.
    //$out = wp_remote_get( $url, $args );

    // Save the API response so we don't have to call again until  tomorrow.
    set_transient( 'lusso_posts', $json, DAY_IN_SECONDS );

    // Return the list of subscribers.  The function will return here the first time it is run, and then once again, each time the transient expires.
    return $json;

  }

}`

Any help would be appreciated.

Thanks.

8
  • What is the output of your var_dump($json)? Can you try echo ($json->{'wp-json'}->posts[1]->ID);? Dec 1, 2015 at 22:04
  • The output of var_dump($json) is my wordpress blog posts in json format. I tried your solution and even tried to var_dump it and it returns null value :( Dec 1, 2015 at 22:10
  • Check if $json is valid (object/not null) and then echo.
    – luke
    Dec 1, 2015 at 22:33
  • Does wp-json show up in your var_dump? Is line 22 the var_dump or the echo? What does your lusso_posts code look like? Dec 1, 2015 at 22:38
  • This is my code for the Lusso function - im using a transient to get post data from another website and show it on my own website by caching it. See my edit on my post; Dec 1, 2015 at 23:06

1 Answer 1

1

I managed to solve this and access the JSON data using a foreach loop:

$json = lusso_posts();
#var_dump( $json );
#die();

foreach( $json as $post ) { 
    $titles = $post->title;
    $images = $post->featured_image->guid;
    ?>
    <div class="lusso-posts">
        <div class="image-container"><img src="<?php echo $images; ?>" />    </div>
        <h4><?php echo $titles; ?></h4>
    </div>  
    <?php
}

Your Answer

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

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