I want to add image from post content in my rss feed but all the tutorials that I find are only for featured image. I want image from post content and not featured image. How can I do this?
3 Answers
Here is the link I found a solution. How to display featured post thumbnails in WordPress feeds
paste this code snippet in your theme functions.php file
// display featured post thumbnails in WordPress feeds
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
//if you want to show thumbnail image
$content = '<figure>' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '</figure>' . $content;
// if you want to show full image
//$content = '<p>' . get_the_post_thumbnail( $post->ID,'full' ) . '</p>' . $content;
//if you want to custom image using add_image_size()
//$content = '<figure>' . get_the_post_thumbnail( $post->ID, 'custom-image-size' ) . '</figure>' . $content;
}
return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
This is the concepts how to you show external link image. How you fetch the external image, it upon you.
it's showing all the post same image. Just change image link for each post. ex: src="'.$image_url.'"
function rss_feed_from_external_link( $content ) {
global $post;
$content = '<figure><img width="150" height="128" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" sizes="100vw" /></figure>' . $content;
return $content;
}
add_filter( 'the_excerpt_rss', 'rss_feed_from_external_link' );
add_filter( 'the_content_feed', 'rss_feed_from_external_link' );
Image showing for custom field.
add_filter('the_content', 'custom_fields_for_feeds');
add_filter('the_meta','custom_files_for_feed');
function custom_fields_for_feeds( $content ) {
global $post;
global $post;
// Get the custom fields ***
// Checks for thumbnail
$image = get_post_meta($post->ID, 'Thumbnail', $single = true);
// Checks for thumbnail alt text
$image_alt = get_post_meta($post->ID, 'Thumbnail Alt', $single = true);
if($image_alt == '') { $image_alt = 'This image has no alt text'; }
if($image !== '') {
$content = '<figure><img src="'.$image.'" alt="'.$image_alt.'" /></figure>' . $content;
return $content;
}
else {
$content = $content;
return $content;
}
add_filter('the_content', 'custom_fields_for_feeds');
add_filter('the_meta','custom_files_for_feed');
You can also use custom image fields to display in the RSS feed.
Let me know you not solve your problem.
-
I don't use featured image or custom fields. My images are hosted external so I want it to display the first image in the post content on my rss. Commented Mar 11, 2017 at 23:14
-
It's very easy... ok I am updating my answer. For your questions my answer is correct. You should update your questions. By the by I am updating your questions. Commented Mar 12, 2017 at 14:48
-
I updated my code, If You fetch any trouble please let me know and also brief details. Thanks Commented Mar 12, 2017 at 15:28
This code is working like a charm, you can add a custom image without affecting any page (the image only shown in the RSS feed file).
function rss_feed_from_external_link( $content ) {
global $post;
$content = '<figure><img width="100%" height="auto" src="img_src" class="" alt="" /></figure>' . $content;
return $content;
}
add_filter( 'the_excerpt_rss', 'rss_feed_from_external_link' );
add_filter( 'the_content_feed', 'rss_feed_from_external_link' );
Thank you
If you want to use content images on other website or pages you may use WordPress REST API. This will help your access all data of the content.