(07) 3040 6690

Add Images to Your WordPress Summary Feed

By: , On Monday, August 31st, 2009

wordpressThere are different reasons to use excerpts instead of full content in your rss feeds, Such as discouraging sploggers from stealing your content, and encouraging feed subscribers to visit your site. A drawback of this though is that WordPress by default only uses the first 55 words of your post in an excerpt minus any image or other media files you may have included in your post. One way around this is to enter your own custom excerpt into the excerpt field of your post editor while in HTML mode. Another method which I’m now using automatically adds the first image in your post (or a default image you specify if there is no image in your post) to your rss content, involves a function I found at Live Experience . Simply add the following code to your wp-includes/functions.php file:

function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img ="http://yoursite.com/image.png";
}
return $first_img;
}

Then open up wp-includes/feed-rss2.php or the file name that corresponds to which ever feed format you’re using, next find each instance of the following line of code

and append the following to the front of it:

' ?>

That’s all there is to it, if you’ve done everything right you should now automatically have images in your feeds, if you use a them which uses excerpts I’m sure you can think of at least one other way to use this function as well ;).

Update: The steps above involve editing some of the WordPress core files. The main problem with this is that you lose your changes whenever you update WordPress. A slightly modified and easier way is to simply place the following code into your themes functions.php file.

function get_first_image($content) {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img ="http://yoursite.com/image.png";
}
return '
'.$content;
}
add_filter('the_excerpt_rss', 'get_first_image');
?>

If you should receive a php headers error, check to make sure there are no spaces or empty lines between any of the php code blocks in your functions.php file


Fatal error: Call to undefined function adsensem_ad() in /home/content/14/4543214/html/wordpress/wp-content/themes/wardelldesign/single.php on line 129