Just a basic how-to here: making your own Feed importer tool to add some feeds in your WordPress themes. WordPress offers a bunch of useful tools to manage feeds, and among other cool stuffs, a method to import feeds : fetch_feed()
. Given the feed’s url, you get back the feed content. Piece of cake 🙂 This goes in your theme’s functions.php
:
function get_friends_feeds($friends = array(), $display = 2) { include_once(ABSPATH.WPINC.'/feed.php'); $feeds = array(); if (count($friends) != 0) { foreach($friends as $friend) { $rss = fetch_feed($friend); if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity($display); $rss_items = $rss->get_items(0, $maxitems); endif; if ($maxitems > 0) { foreach($rss_items as $item ) { $feeds[$item->get_date('U')] = array('_href' => esc_url($item->get_permalink()), '_title' => esc_html($item->get_title()), '_desc' => strip_tags($item->get_description()), '_date' => $item->get_date('j<br />M')); } } } } return $feeds; }
Note that this part only returns an array containing the feeds you want as objects; what to do with them depends on what you want. I use it to show me a simple bulleted list. This goes in my theme’s home.php
, put it wherever you want:
<?php $feeds = get_friends_feeds(array('http://feeds.boston.com/boston/bigpicture/index', 'http://fluxbb.org/forums/extern.php?action=feed&type=atom')); krsort($feeds); ?> <?php if (count($feeds) != 0) : ?> <div> <ul> <?php foreach($feeds as $feed) : $feed = (object) $feed; ?> <li> <?php echo $feed->_date; ?>: <a href="<?php echo $feed->_href; ?>" title="<?php echo $feed->_title; ?>"><?php echo $feed->_title; ?><br /> <?php echo long_excerpt($feed->_desc, 25); ?></a> </li> <?php endforeach; ?> </ul> </div> <?php endif; ?>
get_friends_feeds()
takes two parameters: first is an array containing all your feeds url, second is an integer to limit the returned items. Default is 2. Pretty simple way to get a few feeds to dynamize a bit your blog 🙂 I might make some Widget integration of that thing so that it can be include directly from Dashboard without editing the templates’ code. Feel free to use and abuse!
Note: the ksort()
call on $feeds
could be done directly into the get_friends_feeds()
method; I chose not to do so to keep the liberty to order feeds’ items by date on a global scale or to let them ordered by feed if I want to. Anyway, you may modify this code however you want, so… Go ahead!