user3130064
user3130064

Reputation: 1071

Display posts feed from wordpress using json doesn't working

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.

Upvotes: 2

Views: 516

Answers (1)

pavlovich
pavlovich

Reputation: 1942

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>';

Upvotes: 2

Related Questions