Get WordPress blog RSS feed and show on any page

Apr 26, 2016, Tags - WordPress

We may need to get the WordPress blog RSS feed on some other pages out of WordPress. Let's see how to achieve this.

First, we need WordPress blog RSS feed URL. Generally, the URL is http://example.com/feed or

http://example.com/?feed=rss
http://example.com/?feed=rss2
http://example.com/?feed=rdf
http://example.com/?feed=atom
http://example.com/wp-rss.php
http://example.com/wp-rss2.php
http://example.com/wp-rdf.php
http://example.com/wp-atom.php

Now move to the second step. We simply load the RSS XML by using function - simplexml_load_file()

This will fetch the XML feed from WordPress. $blog_rss->channel->item will contain all the posts, to show them, we can use foreach() loop to display posts on the desired page.

Copy the below code snippets and paste it to the desired page, then simply replace the feed URL with your WordPress feed URL.

<?php
$feed_url='https://example.com/feed'; // feed URL
$blog_rss = simplexml_load_file($feed_url); // Fetch the RSS XML 
?>

<div class='blogFeed'>
    <h3><?=$blog_rss->channel->title?></h3>
    <?php foreach ($blog_rss->channel->item as $item): ?>
        <div class='feedPost'>
            <div class='heading'><a href="<?=$item->link?>"><?=$item->title?></a></div>
            <span class='badge'><?=$item->pubDate?></span>
            <p><?=$item->description?></p>
        </div>
    <?php endforeach; ?>
</div>

$blog_rss->channel->title : This is the channel title, like your site title.

$blog_rss->channel->item : This contains the posts

$item->link : This is the URL of the post

$item->pubDate : This is the published date of post

$item->description : This is the description or excerpt of the post

That is all.


Tags - WordPress

Gautam Kumar

Editor

Leave a comment