user3473050
user3473050

Reputation: 21

How to display WordPress RSS feed on a non wordpress page?

I know this has been posted before, but none of the answers are working for me. I am missing something and would appreciate any help.

I have WordPress installed in a folder called "news" and it is working. I would like to display recent blog posts via RSS on a different page (the page is not generated by WordPress).

I am getting the following error:

Fatal error: Call to a member function getElementsByTagName() on a non-object

Here is the code I'm using:

$xml=("http://www.williamsargent.info/avadasoftware/news?feed=rss2");

$xmlDoc = new DOMDocument();

$xmlDoc->load($xml);

$channel=$xmlDoc->getElementsByTagName('channel')->item(0);

$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;

Upvotes: 2

Views: 397

Answers (1)

Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

You can use following, this is more readable;

$feed = new DOMDocument();
$feed->load('http://www.williamsargent.info/avadasoftware/news?feed=rss2');


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

Upvotes: 1

Related Questions