0

I'm trying to import a feed from a WordPress site to another using fetch_feed(). All is good except for the fact I can get only the first item of the feed.

Here's the code I'm using

add_shortcode('custom_feed','feed2');
 function feed2(){
    $rss = fetch_feed( 'http://blog.sugarpulp.it/feed/' );
    $rss_items = $rss->get_items( 0, 3 );
    foreach ( $rss_items as $item) {
        $title      = $item->get_title();
        return $title;
    }
 }

Hope can help me out. This thing is driving me crazy!

1 Answer 1

1

It looks like the problem is from this line return $title; inside the foreach loop.

Try something like this instead:

add_shortcode('custom_feed','feed2');
 function feed2(){
    $rss = fetch_feed( 'http://blog.sugarpulp.it/feed/' );
    $rss_items = $rss->get_items( 0, 3 );
    $out = array();
    foreach ( $rss_items as $item) {
        $out[] = $item->get_title();
    }
    return join( ' ', $out );
 }

using only a single return after the loop.

From the PHP manual on return :

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call.

You can read more about it here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.