Skip to content

Instantly share code, notes, and snippets.

@sunriseweb
Created September 18, 2014 19:04
Show Gist options
  • Save sunriseweb/5bc71f7bbcfc11952361 to your computer and use it in GitHub Desktop.
Save sunriseweb/5bc71f7bbcfc11952361 to your computer and use it in GitHub Desktop.
Display RSS Feed in WordPress
/**
* Display RSS feed
* - can be called via shortcode
*/
function display_rss_feed($args) {
extract(shortcode_atts(array(
"rssurl" => 'http://incontactunitedchurch.blogspot.com/feeds/posts/default?alt=rss'
), $atts));
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( $rssurl );
if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity( 5 );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
endif;
$result = '';
if ( $maxitems == 0 ) :
$result .= '<article class="rssfeed">';
$result .= '<h2>'.__( 'No items' ).'</h2>';
$result .= '</article>';
else :
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) :
$result .= '<article class="rssfeed">';
$result .= '<h2>';
$result .= '<a href="'.esc_url( $item->get_permalink() ).'" title="'.__( 'Posted ' ).$item->get_date('j F Y | g:i a').'">';
$result .= esc_html( $item->get_title() );
$result .= '</a>';
$result .= '</h2>';
$authorname = '';
if ($author = $item->get_author()) {
//If blogspot.com in url then get name from email
if( strpos($rssurl, 'blogspot.com') ) {
$authorname = $author->get_email();
$namestarts = strpos($authorname, '(')+1;
$namelength = strlen($authorname)-$namestarts-1;
$authorname = substr( $authorname, $namestarts, $namelength);
} else {
$authorname = $author->get_name();
}
}
$result .= '<p class="post-meta">'.__('by ').$authorname.' | '.$item->get_date('M d, Y').'</p>';
$result .= '<div class="rsscontent">'.$item->get_content().'</div>';
$result .= '</article>';
endforeach;
endif;
return $result;
}
add_shortcode('displayRSSfeed', 'display_rss_feed');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment