Get Wordpress Featured image form external RSS feed

1.3k Views Asked by At

I used the following code in my FUNCTIONS file:

add_action('rss2_item', 'add_my_rss_node');

function add_my_rss_node() {
    global $post;
    if(has_post_thumbnail($post->ID)):
        $thumbnail = get_attachment_link(get_post_thumbnail_id($post->ID));
        echo("<image>{$thumbnail}</image>");
    endif;
}

Now I try to call that image and display it from site A to my site B with a custom RSS tempalte. This is the code of the template.

<?php get_header(); ?>

<?php include_once(ABSPATH.WPINC.'/feed.php');
$rss = fetch_feed('https://notrealurl.net/categoryname-feed/');
$maxitems = $rss->get_item_quantity(30);
$rss_items = $rss->get_items(0, $maxitems);
?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>

<li>
<a href="<?php echo $item->get_permalink(); ?>">
<?php echo $item->get_title(); ?> 
<?php echo $item->get_date('j F Y @ g:i a'); ?>


</a>
</li>
<?php endforeach; ?>
</ul>

<?php get_footer(); ?>

I see the image url in my rss feed now ( thanks to that functions script) and it is in a tag.

Looks like this: https://www.mywebsite./url-of-the-image/

They dont have a .jpg or .png extension at the end of the url. Not sure if that is how it needs to be. When I open that link I can see the image, and it works.

Can somebody please help me to figure this out ? I'm working on this thing for 2 days and can't figure it out. My PHP isn't great so that is most likely the reason why I can't figure it out.

Thanks in advance :)

1

There are 1 best solutions below

4
Mike C. On

I think you want your function to get the attachment src instead of the link, and echo that src into a <url> tag inside the <image> RSS element (see here). Try this:

function add_my_rss_node() {
    global $post;
    if(has_post_thumbnail($post->ID)):
        $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
        echo("<image><url>{$thumbnail}</url></image>");
    endif;
}