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

Nouveau short sur Blooness 👉 Relancer la libido, en vrac -> Soleil, vitamine D, diète céto-méditerranéenne, protéines, livres, badinage, marche, HIIT, et pas de régime hypocalorique.
https://buff.ly/445PWvc

Nouveau short sur Blooness 👉 Améliorer son sommeil -> s’exposer à la lumière le matin, limiter les lunettes de soleil, mouiller la chemise au sport, boire du déca, manger maximum 3h avant de dormir, faire une marche digestive et prendre de la mélaton
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