Domanda

I have a foreach loop like this:

$SimplePie->set_feed_url(array('links'));
$SimplePie->init();
$SimplePie->handle_content_type();
foreach ($SimplePie->get_items() as $item) {
    echo $item->get_image_url();
}

But I always get this error Call to undefined method SimplePie_Item::get_image_url(). The same thing is if I try any else method which is starting with get_image... I printed out whole $item variable and I found that inside it there is url for image, but I don't know what is wrong that I can't get url with this method.

What am I doing wrong?

È stato utile?

Soluzione 2

After a little of hacking SimplePie library I came across that it doesn't have support for posts's images yet. It's really easy to get images out of posts, with already created function get_item_tags() which allows you to browse SimplePie's parsed output. So here is the code for getting first image of all if it exists:

$image = $item->get_item_tags('', 'image')[0]['child']['']['url'][0]['data']; // manually getting the first image's url
if (isset($image)) // if exists
{
    echo $image; // do something with it
}

If you want to get more images, just put $item->get_item_tags('', 'image') in foreach and get out the links.

I hope that SimplePie authors will implement function for this tag as soon as possible :)

Altri suggerimenti

You have a missing semicolon at the end of that line.

echo $item->get_image_url() // need to close this line

echo $item->get_image_url();  // correct

Other than that, what version of SimplePie are you using?

EDIT: Upon further investigation, this function is for the feed, not feed items. So what you need to do in your item loop is this:

$parent_feed = $item->get_feed();
$parent_feed->get_image_url();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top