How to generate list of links from external WordPress Feed
WordPress handily provides a RSS feed, usually at /feed/
This is accessible externally so it’s easy to use this data on external or local sites for simple link lists, scraping etc.
Here’s a really simple way to generate a li
list of article headings along with their relevant link from an external WordPress feed.
Just replace the URL within the $feedURL
variable to your desired feed.
<ul>
<?php
$feedURL = 'https://guwii.com/news/feed/';
$rss = new DOMDocument();
$rss->load($feedURL);
$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);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
echo '<li><a href="'.$link.'" title="'.$title.'">'.$title.'</a></li>';
}
?>
</ul>