Created
September 4, 2015 15:20
-
-
Save deepak-rajpal/eedca193c751b5cd6587 to your computer and use it in GitHub Desktop.
RSS XML News Feeds Fetch using WordPress fetch_feed() - Best and Tested
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* =============== Starts: Shortcode to display RSS Feeds using WordPress fetch_feed() ================== */ | |
/* Shortcode Example: [news_list] | |
Example Feed URL: | |
=> 'http://online.wsj.com/xml/rss/3_8068.xml' | |
=> 'http://dealbook.nytimes.com/feed/' | |
=> 'http://www.bloomberg.com/feed/bview/' | |
*/ | |
function news_page_fx( $atts ) { | |
extract( shortcode_atts( array( | |
'feed_url' => 'http://online.wsj.com/xml/rss/3_8068.xml', | |
'title' => 'Market Headlines', | |
), $atts ) ); | |
ob_start(); | |
$feed_url = htmlspecialchars_decode($feed_url); | |
$rss = fetch_feed($feed_url); | |
if ( ! is_wp_error( $rss ) ) : | |
$maxitems = $rss->get_item_quantity( 20 ); | |
$rss_items = $rss->get_items( 0, $maxitems ); | |
foreach ( $rss_items as $item ) : | |
$news_title = $item->get_title(); | |
$news_date = $item->get_date('j F Y | g:i a'); | |
$news_link = $item->get_permalink(); | |
$news_description = $item->get_description(); | |
?> | |
<div class="entry"> | |
<div class="entrycontent"> | |
<h2><a href="<?php echo $news_link; ?>" target="_blank"><?php echo $news_title; ?></a></h2> | |
<p><span class="date"><?php echo $news_date; ?></span></p> | |
<p><?php echo $news_description; ?></p> | |
</div> | |
</div> | |
<?php | |
endforeach; | |
endif; | |
libxml_use_internal_errors(false); | |
$output_string = ob_get_contents(); | |
ob_end_clean(); | |
return $output_string; | |
} | |
add_shortcode( 'news_list', 'news_page_fx' ); | |
/* Ends: Shortcode to display RSS News Feeds */ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment