Custom RSS feed - not getting the featured image

I have a custom feed template named feed-short.php (code is below), that you can view at https://www.alanizmarketing.com/blog/?feed=short. The problem here is that I am not getting the featured image pulled into the feed. Each post has a different featured image. What am I missing in the (Feed defaults) $postimages?? I tried everything I can think of, but haven't solved this yet.

<?php
/**
* Custom RSS template.
*
* @package sometheme
* @subpackage theme
*/

/**
* Feed defaults.
*/
header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
$frequency = 1; // Default '1'. The frequency of RSS updates within the update period.
$duration = 'hourly'; // Default 'hourly'. Accepts 'hourly', 'daily', 'weekly', 'monthly', 'yearly'.
$postlink = '
See the rest of the story at mysite.com

';
$email = get_the_author_meta( 'email');
$author = get_the_author();
$postimages = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'medium' );
// Check for post image. If none, fallback to a default.
$postimage = ( $postimages ) ? $postimages[0] : get_stylesheet_directory_uri() . '/images/default.jpg';

/**
* Start RSS feed.
*/
echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>

<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action( 'rss2_ns' ); ?>
>

<!-- RSS feed defaults -->
<channel>
<title><?php bloginfo_rss( 'name' ); wp_title_rss(); ?></title>
<link><?php bloginfo_rss( 'url' ) ?></link>
<description><?php bloginfo_rss( 'description' ) ?></description>
<lastBuildDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></lastBuildDate>
<language><?php bloginfo_rss( 'language' ); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', $duration ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', $frequency ); ?></sy:updateFrequency>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />

<?php do_action( 'rss2_head' ); ?>

<!-- Start loop -->
<?php while( have_posts() ) : the_post(); ?>

<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<author><?php echo $email ?><?php echo ' (' . $author . ')' ?></author>
<image>
<url><?php echo esc_url( $postimage ); ?>"/></url>
</image>
<pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>

<!-- Echo content -->
<content:encoded>
<![CDATA[<?php echo the_excerpt(); echo $postlink; ?>]]>
</content:encoded>
</item>

<?php endwhile; ?>
<!-- End loop -->
</channel>
</rss>

  • Panos

    Hi Larry Levenson ,

    You need to move the part:

    $postlink = '
    See the rest of the story at mysite.com
    
    ';
    $email = get_the_author_meta( 'email');
    $author = get_the_author();
    $postimages = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'medium' );
    // Check for post image. If none, fallback to a default.
    $postimage = ( $postimages ) ? $postimages[0] : get_stylesheet_directory_uri() . '/images/default.jpg';

    inside the loop, right after the line:
    <?php while( have_posts() ) : the_post(); ?>

    Currently these vars will get wrong values, as they are out of loop, so not only the image is wrong but also the rest information for author, email etc.

    Please let me know if this helps!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.