Tulip Media

Entrepreneurial thoughts

WordPress tip: put images in the RSS feed

Here is a very simple code that will allow you to insert your featured image into 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;
}

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');

 

My latest tweets

New shorts on Blooness 👉 Vitality, longevity, weight loss: the ultimate guide to proteins in the keto-Mediterranean diet is online on Blooness. https://buff.ly/3xxNvpj
https://buff.ly/4cG2GM4

New shorts on Blooness 👉 The new premium content comes out in 48 hours, and what content! This will be the most comprehensive guide on the amount of protein to consume on a keto-Mediterranean diet. See you soon !
https://buff.ly/3L2KbFP

New Post: Combo, the best solution to put an end to the headache of scheduling and HR management https://buff.ly/4eE6JtT

Load more

Don't miss business advice by email

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

4 Responses

Leave a Reply

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

en_USEN