Created
October 3, 2015 10:54
-
-
Save pradeepdotco/761c6bc617b53ea6fc38 to your computer and use it in GitHub Desktop.
Function to add featured image to RSS feed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'the_content', 'featured_image_in_feed' ); | |
function featured_image_in_feed( $content ) { | |
global $post; | |
if( is_feed() ) { | |
if ( has_post_thumbnail( $post->ID ) ){ | |
$output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) ); | |
$content = $output . $content; | |
} | |
} | |
return $content; | |
} | |
?> |
Is it still valid? are this will work straight away or need to wait 24h for rss readers to refresh?
replace
$output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
with this
$output = the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
I would rather use 'the_content_feed' filter, and 'the_excerpt_rss' filter to make it work with excerpts as well. Then there's no need for is_feed() check, because filters are specific for feed. And we need to check if the feed is for comments or for posts. We don't need post's featured image in comments feed.
<?php
add_filter( 'the_content_feed', 'featured_image_in_feed' );
add_filter( 'the_excerpt_rss', 'featured_image_in_feed' );
function featured_image_in_feed( $content ) {
global $post, $wp_query;
if( ! $wp_query->is_comment_feed) {
if ( has_post_thumbnail( $post->ID ) ){
$output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
$content = $output . $content;
}
}
return $content;
}
?>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it didn't worked for me :(