Skip to content

Instantly share code, notes, and snippets.

@dangayle
Created November 6, 2012 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dangayle/7dd8a245658c80c136a4 to your computer and use it in GitHub Desktop.
Save dangayle/7dd8a245658c80c136a4 to your computer and use it in GitHub Desktop.
The correct way to import feeds into WordPress using fetch_feed()
<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://dangayle.com/feed/';
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
if ($rss_items):
echo "<ul>\n";
foreach ( $rss_items as $item ) :
//instead of a bunch of string concatenation or echoes, I prefer the terseness of printf
//(http://php.net/manual/en/function.printf.php)
printf('<li><a href="%s">%s</a><p>%s</p></li>',$item->get_permalink(),$item->get_title(),$item->get_description() );
endforeach;
echo "</ul>\n";
endif;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment