Tulip Media

Entrepreneurial thoughts

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;
}

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 👉 Relaunch libido, in bulk -> Sun, vitamin D, keto-Mediterranean diet, proteins, books, banter, walking, HIIT, and no hypocalorie diet.
https://buff.ly/445PWvc

New shorts on Blooness 👉 Improve your sleep -> expose yourself to light in the morning, limit sunglasses, wet your shirt during sports, drink decaf, eat maximum 3 hours before sleeping, take a digestive walk and take some melaton
https://buff.ly/440weAJ

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