Add Images to Your WordPress Summary Feed

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('/<img.+src=[\'"]([^\'"]+)[\'"].*>/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
<?php the_excerpt_rss() ?>

and append the following to the front of it:

<?php echo '<img src="'.get_first_image().'" alt="" />' ?>

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 ;) .

  • Delicious
  • Google Buzz

Related posts:

  1. Redirect a WordPress Post to its Permalink



© Wardell Design 2010
Entries (RSS) and Comments (RSS).