2

I'm trying to display some posts in my PHP website from another Wordpress website that uses the WP REST API plugin installed. I'm trying to do that with that code below but nothing happens:

         <?php 
            $json = file_get_contents('http://noticias.uscs.edu.br/wp-json/wp/v2/posts?filter[posts_per_page]=6&filter[orderby]=date');
            // Convert the JSON to an array of posts
            $posts = json_decode($json);

            foreach ($posts as $p) {
             echo '<p>Title: ' . $p->title . '</p>';
             echo '<p>Date:  ' . date('F jS', strtotime($p->date)) . '</p>';
             // Output the featured image (if there is one)
             echo $p->featured_image ? '<img src="' . $p->featured_image->guid . '">' : '';
            }

          ?>

Any opinion? Thanks in advance.

1 Answer 1

2

On this line echo '<p>Title: ' . $p->title . '</p>'; $p->title is an object so you have to fix it by going a bit further like this:

echo '<p>Title: ' . $p->title->rendered . '</p>';

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! The error disappear. But, the posts list still doesn't appears correctly. All that this snippet is showing is this: s28.postimg.org/f2j8u7vxp/…
@user3130064 but there there seems to be only 1 post result with your query
I found the problem. I've filtered by pages instead posts, now it's alright! The last one thing. The featured image isn't showing, do you know what might be? Thanks in advance.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.