Add Images to Your WordPress Summary Feed

By: Wardell, 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('/<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 ;) .

Related posts:

  1. Redirect a WordPress Post to its Permalink
  2. WordPress 2.7 Released
  3. Create a Rollover Class with JavaScript
  4. WordPress Permalinks & Godaddy Subdomain
  5. Firefox Add-ons for Better Browsing & Search

4 Responses to “Add Images to Your WordPress Summary Feed”

  1. Fernando Says:

    Hi,
    I am trying to do what is explained on the post, but it is not working. What could I do? Could you helo me, please?
    Thanks in advance,
    Fernando

    Reply

    Wardell
    Twitter: @
    Reply:

    Hi Fernando, I’ve just realized that my blog had striped some of the characters from the function above but this has been corrected and the function should work for you now.

    Reply

  2. Tevya
    Twitter: @
    Says:

    I tried this, but it doesn’t seem to be working. Could it be that it doesn’t work with WordPress 3? Or perhaps its the theme Suffusion? Any ideas would be helpful. The site I’m using it on is http://www.mormonlifehacker.com
    Thanks.
    Tevya´s last blog ..Affiliate Banners- Buttons- &amp SkyscrapersMy ComLuv Profile

    Reply

  3. Tevya
    Twitter: @
    Says:

    You can disregard my previous comment. It is working! Guess it just took a little while to propagate. So is there a way to just have it show the thumbnail or even small size of the image? Instead of the full size? Some images I put in posts are rather large, but I just put a small version with a link to the full size. If there were a way to just insert the thumbnail or small size, that would be much better for readers. Thanks.
    Tevya´s last blog ..Affiliate Banners- Buttons- &amp SkyscrapersMy ComLuv Profile

    Reply

Leave a Reply

CommentLuv Enabled
:) :D :-h :rock: =)) =D> B-) ;) :-bd :-q :x :| :( :(( x( :p :~( :-w ~O) :-B P-) :-?? *-:) ^:)^ more »


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