Search code examples
phpwordpressimagewordpress-themingrss

WordPress featured image url in RSS feed template file


I have created a custom RSS feed template file:

<?php
/**
 * RSS 0.92 Feed Template for displaying RSS 0.92 Posts feed.
 *
 * @package WordPress
 */

header( 'Content-Type: ' . feed_content_type( 'rss' ) . '; charset=' . get_option( 'blog_charset' ), true );
$more = 1;

echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>

<articles>
<?php
while ( have_posts() ) :
    the_post();
    ?>
    <article>
        <title><?php the_title_rss(); ?></title>
        <teaser><?php the_excerpt_rss(); ?></teaser>
        <description><?php $original = strip_tags(get_the_content_feed('rss2'));
        $stripped = str_replace("&nbsp;", " ", $original);
        echo $stripped; ?></description>
        <link><?php the_permalink_rss(); ?></link>
        <image>http://placehold.it/100x100</image>
        <date><?php 
    echo mysql2date(
        'd-m-Y H:i:s', 
        get_post_time('Y-m-d H:i:s', true), 
        false
    ); 
?></date>
        <?php
        /**
         * Fires at the end of each RSS feed item.
         *
         * @since 2.0.0
         */
        do_action( 'rss_item' );
        ?>
    </article>
<?php endwhile; ?>
</articles>

I am having an issue with I want it to output featured image url but atm. it's just hardcoded with a placeholder image.

I have tried with get_the_post_thumbnail URL like so:

<image><?php echo get_the_post_thumbnail_url('full'); ?></image>

However the echo is just blank.

I have also tried below but it just echo array.

<image><?php echo wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?></image>

How can I echo featured image URL in my RSS feed?


Solution

  • If you want to use get_the_post_thumbnail_url, pass the id to it like so:

    <image>
       <url><?php echo get_the_post_thumbnail_url(get_the_ID(), 'full'); ?></url>
    </image>
    

    Or if you want to use wp_get_attachment_image_url, then you could do something like this:

    <image>
       <url><?php echo wp_get_attachment_image_url(get_the_ID(), 'full'); ?></url>
    </image>