Add an Image to WordPress RSS Feed

We are going to add an image to the WordPress RSS feed, since by default this CMS content manager does not show the default image of the entry or publication we make.

To do this, in the functions.php file of our child theme we are going to introduce the following code shown below:

/** Add image to RSS feed */
function post_thumbnails_in_feeds( $content ) {
global $post;
is_feed ($feeds = ''); {
if( has_post_thumbnail( $post->ID ) ) {
$content = '<figure>' . get_the_post_thumbnail( $post->ID, 'medium', array('alt' => get_the_title()) ) .
 '</figure>' . $content;
return $content;
add_filter( 'the_excerpt_rss', 'post_thumbnails_in_feeds' );

In this code, the get_the_post_thumbnail function is used to call the image assigned to the post, in the case of the example with the medium size and the alt tag that shows the title of the post.

All of this, wrapped in the <figure> tag, the same one that will appear within the <description> tag when the WordPress RSS feed is presented.

After you add an image to the WordPress RSS feed, it should display at the following URL on your website:

5/5 - 3 votes

Discover more from Politikaperu

Subscribe to get the latest posts sent to your email.

Discover more from Politikaperu

Subscribe now to keep reading and get access to the full archive.

Continue reading