WordPress: includere un feed RSS nei post e nelle pagine

WordPress: includere un feed RSS nei post e nelle pagine

In WordPress possiamo includere un feed RSS nelle pagine e nei post con un semplice shortcode.

La soluzione è la seguente:


function my_feed( $atts, $content = null ) {
	extract( shortcode_atts( array(
		'url' => '',
		'limit' => '5'
	), $atts ) );

	include_once( ABSPATH . WPINC . '/feed.php' );

	$rss = fetch_feed( $url );
	$html = '';

	if ( !is_wp_error( $rss ) ) {
		$maxitems = $rss->get_item_quantity( $limit );
		$rss_items = $rss->get_items( 0, $maxitems );

		$html = '<ul class="my-feed">';

		foreach ( $rss_items as $item ) {
			$html .= '<li><a href="' . esc_url( $item->get_permalink() ) . '">' . esc_html( $item->get_title() ) . '</a></li>';
		}

		$html .= '</ul>';
	}

	return $html;

}

add_shortcode( 'my-feed', 'my_feed' );

Torna su