Skip to content

Instantly share code, notes, and snippets.

@peterhartree
Last active December 28, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhartree/7500741 to your computer and use it in GitHub Desktop.
Save peterhartree/7500741 to your computer and use it in GitHub Desktop.
How to use the SimplePie RSS library to display a list of linked article headlines from a Wordpress RSS feed on a non-Wordpress PHP page.
<?php
/** Outputs a list of linked article headlines from a Wordpress recent posts RSS feed. */
$simplepie_path = "includes/simplepie/"; // path to SimplePie RSS library
$feed_url = 'http://yourblog.com/feed/'; // url of your blog's RSS feed
$cache_location = $_SERVER['DOCUMENT_ROOT'] . '/includes/simplepie-cache'; // set cache directory (make sure that the directory exists and that this script can write to it)
$max_items = 3; // max feed items to show
$first_item = 0; // feed item to start from
require_once($simplepie_path.'autoloader.php');
$feed = new SimplePie();
$feed->set_feed_url($feed_url);
$feed->set_cache_location($cache_location);
$feed->init();
$feed->handle_content_type();
if($feed->error()):
// something went wrong
echo $feed->error();
else:
// output list of feed items
echo '<ul>'."\n";
foreach ($feed->get_items($first_item, $max_items) as $item):
echo ' <li><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></li>'."\n";
endforeach;
echo '</ul>'."\n";
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment