Skip to content

Instantly share code, notes, and snippets.

@mwender
Created November 26, 2013 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwender/7663428 to your computer and use it in GitHub Desktop.
Save mwender/7663428 to your computer and use it in GitHub Desktop.
Revised shortcode handling for a recent posts widget that pulls content from an RSS feed. For use in a child theme of Avada.
add_shortcode('mcm_recent_feed_posts', 'shortcode_mcm_recent_feed_posts');
/**
* shortcode_mcm_recent_feed_posts() - Retrieves posts from RSS feed
*
* @see fetch_feed()
*
* @since 1.0.0
*
* @param array $atts {
* @type string $url RSS feed URL.
* }
* @return string Feed recent posts html.
*/
function shortcode_mcm_recent_feed_posts( $atts, $content = null ){
extract( shortcode_atts( array(
'excerpt_words' => null,
'url' => null,
), $atts) );
$args = array();
if( is_null( $url ) )
return '<p>Feed URL is null.</p>';
$feed = fetch_feed( $url );
if( ! is_wp_error( $feed ) ){
$maxitems = $feed->get_item_quantity( 3 );
$rss_items = $feed->get_items( 0, $maxitems );
if( 0 == $maxitems ){
return '<p>Feed contains no items.</p>';
} else {
$format = '<div class="avada-container layout-thumbnails-on-side layout-columns-1"><section class="columns columns-1"><div class="holder">%s</div></section></div>';
foreach( $rss_items as $item ){
$full_content = strip_tags( $item->get_content() );
if( ! is_null( $excerpt_words ) && is_numeric( $excerpt_words ) ){
$all_words = explode( ' ', $full_content );
$content = '';
for( $x = 0; $x < $excerpt_words; $x++ ){
$content.= ' ' . $all_words[$x];
}
$content.= ' [...]';
} else {
$content = $full_content;
}
$src = '';
foreach( $item->get_enclosures() as $enclosure ){
if( 'image' == $enclosure->medium && in_array( $enclosure->type, array( 'image/jpeg', 'image/gif', 'image/jpg', 'image/png' ) ) ){
$src = $enclosure->link;
continue;
}
}
$image = '';
if( ! empty( $src ) )
$image = '<div class="flexslider floated-post-slideshow rss-image"><ul class="slides"><li><a href="#"><img src="' . $src . '" /></a></li></ul></div>';
$items[] = '<article class="col clearfix">' . $image . '<div class="recent-posts-content"><h4><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a></h4><ul class="meta"><li>' . $item->get_date( get_option( 'date_format' ) ) . '</li></ul><div class="excerpt-container">' . $content . '</div>' . $enclosure_html . '</div></article>';
}
return sprintf( $format, implode( "\n", $items ) );
}
return '<pre>$feed = <textarea style="width: 300px; height: 500px;">' . print_r( $feed, true ) . '</textarea></pre>';
} else {
return '<p>' . $feed->get_error_message() . '</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment