Wordpress - get an RSS feed, display items with a loop


/ Published in: PHP
Save to your folder(s)

Thanks Dan Gayle.
http://www.dangayle.com/import-feeds-wordpress-fetch-feed/


Copy this code and paste it in your HTML
  1. <?php
  2. include_once(ABSPATH . WPINC . '/rss.php');
  3. $feed = 'http://dangayle.com/feed/';
  4. $rss = fetch_feed($feed);
  5. if (!is_wp_error( $rss ) ) :
  6. $maxitems = $rss->get_item_quantity(3);
  7. $rss_items = $rss->get_items(0, $maxitems);
  8. if ($rss_items):
  9. echo "<ul>\n";
  10. foreach ( $rss_items as $item ) :
  11. //instead of a bunch of string concatenation or echoes, I prefer the terseness of printf
  12. //(http://php.net/manual/en/function.printf.php)
  13. printf('<li><a href="%s">%s</a><p>%s</p></li>',$item->get_permalink(),$item->get_title(),$item->get_description() );
  14. endforeach;
  15. echo "</ul>\n";
  16. endif;
  17. endif;
  18. ?>

URL: http://www.dangayle.com/import-feeds-wordpress-fetch-feed/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.