Pulling posts from another WordPress site

天大地大妈咪最大 提交于 2019-12-02 01:39:32

As you've found, WordPress feeds have some limitations. Since you've asked for an alternative solution, I'd definitely recommend using WP REST API.

Since WP API isn't yet part of the WP Core, you'll want to do the following:

  1. Head to your Plugins panel (on the site you're trying to pull posts from...your personal website) and install WP REST API (WP API).
  2. Activate the plugin
  3. Getting your posts is as easy as going to: http://yoursite.com/wp-json/posts

Since you only want four posts, you can use filters:

http://yoursite.com/wp-json/posts?filter[posts_per_page]=4

To get this JSON into a usable state in PHP:

// Get the JSON
$json = file_get_contents('http://yoursite.com/wp-json/posts?filter[posts_per_page]=4');
// Convert the JSON to an array of posts
$posts = json_decode($json);

You can now digest this $posts array however you want (by looping through it). For example:

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 . '">' : '';
}

More info in the WP API docs.

If you dont want to use WP REST API, you can give a try to Wordpress Developers API.

You will have to authorize Wordpress Jetpack plugin for this. And then enable the REST API.

<?php
$posts = json_decode(file_get_contents("https://public-api.wordpress.com/rest/v1.1/sites/{yoursite.com}/posts"));
//You can use the $posts variable afterwards
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!