WordPress tip: put images in the RSS feed

Here is a very simple code which will allow you to insert your featured image in your RSS feed, so that, when you relay your feed on a social network like Facebook for example, the image can accompany the title and the presentation of your post.

To do this, go to your file functions.php to add these lines of code:

  • If you only want to use the native WordPress function allowing you to feature images, here is the code you need:

function imageRSS($content) {
global $post;
if (has_post_thumbnail($post->ID)){
$content = » . get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'style' => 'float: right; margin:0 0 10px 10px;' ) ) . » . $content;
}
return $content;
}

Don't miss business advice by email

No spam, just an email when new content is published.

add_filter('the_excerpt_rss', 'imageRSS');
add_filter('the_content_feed', 'imageRSS');

  • If in addition to the native feature of WordPress featured images, some of your articles do not have featured images, the plugin Get the picture will scan your articles for any image to use as a featured image. I then suggest this code which, if there is no featured image, will make the plugin work Get the picture to find one possibly inserted in your article:

function imageRSS($content) {
global $post;
if (has_post_thumbnail($post->ID)){
$content = » . get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'style' => 'float: right; margin:0 0 10px 10px;' ) ) . » . $content;
}
else {
$content = » . get_the_image(array('width' => 150, 'image_scan' => true , 'image_class' => », 'meta_key' => array( 'Thumbnail', 'thumbnail' ))) . » . $content;
}
return $content;
}

add_filter('the_excerpt_rss', 'imageRSS');
add_filter('the_content_feed', 'imageRSS');

  • Finally, you can use this code if you want to check if there is an image attached to the article in case there are no featured images:

function show_image_feed($content) {

global $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);

$attachment = get_posts($args);

if (1TP4Attachment) {

$content = '

' . $content. '
ID).' »>Read more'; } return $content; } add_filter('the_excerpt_rss', 'show_image_feed');

4 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

 

What are the best (and worst) meats for healthy longevity? This is the subject of the new chapter devoted to the natural diet of ruminants, and the modifications made by humans, for better and for worse 👉 https://blooness.com/alimentation-ruminants/

Load more

Don't miss business advice by email

No spam, just an email when new content is published.

en_USEN