[PHP - WordPress] Ottenere un feed RSS e visualizzare i record con un loop

Nel tutorial di oggi imparerete come ottenere un feed RSS e visualizzare i record con un ciclo.

 
< ?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://dangayle.com/feed/';
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
        $maxitems = $rss->get_item_quantity(3);
    $rss_items = $rss->get_items(0, $maxitems);
    if ($rss_items):
        echo "<ul>\n";
        foreach ( $rss_items as $item ) : 
            //instead of a bunch of string concatenation or echoes, I prefer the terseness of printf 
            //(http://php.net/manual/en/function.printf.php)
            printf('<li><a href="%s">%s</a><p>%s</p></li>',$item->get_permalink(),$item->get_title(),$item->get_description() );
        endforeach;
        echo "</ul>\n";
    endif;
endif;
?>
 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>