How to feed wordpress posts in a external website

I have a WordPress blog and a PHP website. I intend to feed the last 6 posts from that WordPress blog to this PHP website. Taking the title, url and thumbnail, and all this from two specific post category. Can anyone help me the best and simple way to do that? I’m a front end developer and I’m not so good in PHP =/

Thanks in advance!

Related posts

2 comments

  1. You can try this method

    1. Install WP API

    2. Get the JSON URL of what you want to load, refer to docs

    e.g. http://yourdomain.com/wp-json/wp/v2/pages?filter[posts_per_page]=6&filter[orderby]=date

    1. Display data from JSON using file_get_content and json_decode

    $posts = json_decode(file_get_contents('http://yourdomain.com/wp-json/wp/v2/pages?filter[posts_per_page]=6&filter[orderby]=date'));
    foreach ( $posts as $post ) {
    echo '<a href="'.$post->link.'">'.$post->title->rendered.'</a>';
    }

  2. There is plugins that can help you move the data from and to your WP site.. Check out this link about WP plugins and RSS feeds

    http://mashable.com/2008/09/08/rss-plugins-for-wordpress/#haJPCZ7VPaqG

    An Easy Way to Display an RSS Feed with PHP

    The following code will first create a new DOMDocument() into which we will load the WordPress.org RSS feed.

    $rss = new DOMDocument();
    $rss->load('http://wordpress.org/news/feed/');
    

    Then we will single out certain elements and place them into an array. For this example, I will just fetch the title, description, link and published on date.

    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    

    Finally, we set it to display 5 posts on screen with the titles linking directly to the original post.

    $limit = 5;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        echo '<small><em>Posted on '.$date.'</em></small></p>';
        echo '<p>'.$description.'</p>';
    }
    

    Not too complicated. All you need to change is the feed you want to load (line #3) and the number of posts to display (line #14). Of course, you could always play around with the output to get it styled exactly how you want. That is totally up to you.

Comments are closed.