bloqhead
7/21/2014 - 6:13 PM

A shortcode that grabs a WordPress feed from a specified URL and category. Usage: [blog-grab url="http://website.com" category="uncategorize

A shortcode that grabs a WordPress feed from a specified URL and category. Usage: [blog-grab url="http://website.com" category="uncategorized" count="3"]

<?php

/**
 * RSS Blog Feed
 */
function bsd_feed_grab( $atts, $content = NULL ) {

	// Extract the shortcode's attributes
	extract( shortcode_atts( array(
		'url' 		=> '', // The feed URL (Eg: http://website.com/)
		'category' 	=> 'uncategorized', // The category
		'count' 	=> 5 // The max number of RSS items displayed
	), $atts ));

	// Fetch the RSS feed
	include_once( ABSPATH . WPINC . '/feed.php' );
	$rss = fetch_feed( $url . '/category/' . $category . '/feed/' );

	// Only execute this if the user has entered a URL
	if( !empty( $url ) ) :

		// If there are RSS items present, move forward
		if( !empty( $rss ) ) :
			$maxitems = $rss->get_item_quantity( $count );
			$rss_items = $rss->get_items( 0, $maxitems );
		endif; ?>

		<div class="blog-rss-container">
			<?php foreach ( $rss_items as $item ) : ?>
			<div class="blog-rss-item">
				<h3>
					<a href="<?php echo $item->get_permalink(); ?>" rel="bookmark">
						<?php echo $item->get_title(); ?>
					</a>
				</h3>
				<div class="blog-rss-summary">
					<p><?php echo $item->get_description() ?></p>
				</div>
			</div>
			<?php endforeach; ?>
		</div>

	<?php endif;
}
add_shortcode( 'blog-grab', 'bsd_feed_grab' );