How to display WordPress RSS feed your website

Currently WordPress is being used as a main blogging platform and WordPress websites are taking good rank in SEO. So if you have installed WordPress on your site and site is static, e-commerece, portal you would most probbaly want to display WordPress blog latest post on side column of your website.

Now we have to use RSS feed of WordPress to display latest post. As WordPress is giving this facility to fetch latest post from WordPress database using RSS feed so on your site all you need to do is know how to fetch this data.

Now we have RSS feed url is like bleow

http://www.yourdomainname.com/blog/feed/

As you can see below tags in XML output and I have mentioned the important ones that we need.

<item>

<title>A Deeper Look at Joomla</title>

<link>POST LINK</link>

<comments>COMMETN URL LINK</comments>

<pubDate>PUBLISHED DATE</pubDate>

<category><![CDATA[joomla!]]></category>

<guid isPermaLink=”false”>http://www.yourdomainname.com/blog/?p=211</guid>

<description><![CDATA[ DESCRIPTION WILL BE HERE ]]></description>

<content:encoded><![CDATA[ CONTENT WILL BE HERE ]]></content:encoded>

</item>

So title is the blog post title name.

Link tag is the link to the post.

Comments is for post comment page link

PubDate is the published date of the post

Category is for post category

Description is for short description of some text

content:encoded is for full description of post if we would like to display that.

so now I will demonstrate how to fetch latest post from XML code. below is code.

<?php

$xml=(“http://www.yourdomainname.com/blog/feed/”);

$xmlDoc = new DOMDocument();

$xmlDoc->load($xml);

$x=$xmlDoc->getElementsByTagName(‘item’);

for ($i=0; $i<1; $i++)

{

$item_title = $x->item($i)->getElementsByTagName(‘title’)->item(0)->childNodes->item(0)->nodeValue;

$item_link = $x->item($i)->getElementsByTagName(‘link’)->item(0)->childNodes->item(0)->nodeValue;

$item_comments = $x->item($i)->getElementsByTagName(‘comments’)->item(0)->childNodes->item(0)->nodeValue;

$item_pubDate = $x->item($i)->getElementsByTagName(‘pubDate’)->item(0)->childNodes->item(0)->nodeValue;

$item_category = $x->item($i)->getElementsByTagName(‘category’)->item(0)->childNodes->item(0)->nodeValue;

$item_guid = $x->item($i)->getElementsByTagName(‘guid’)->item(0)->childNodes->item(0)->nodeValue;

$item_description = $x->item($i)->getElementsByTagName(‘description’)->item(0)->childNodes->item(0)->nodeValue;

$item_content = $x->item($i)->getElementsByTagName(‘content’)->item(0)->childNodes->item(0)->nodeValue;

}

?>

You can see first of all we have assigned feed URL in $xml variable. and DOMDocument class is php function. so we can use that one to retrieve entire XML document as root of document tree. We have created object of DOMDocument class. and then load the $xml url content. now we have to parse <item> tag from XML content. so we have pass item tag in getElementsByTagName function so it will fetch all items tags from XML tree which we have load.

Now with all the item tag contents in $x variable. now we will get one by one item tag element values so we need to iterate for loop for separate each item tag from $x. so as you can see we have write down for loop and right now $i is counter and second parameter of for loop I have given like $i < 1 it’s b’coz i want to display only one latest blog post on web page. if you want to display 5 then you have to set $i < 5 or you want to display all blog post then you have to pass $i < count($x) so it will display all of the feed post.

 

Article By: Hidden Web Genius


Leave a Reply

You must be logged in to post a comment.

Three Measures To Keep Track Of A Cell Mobile Phone appforspy.com mobile tracker app