Custom Field Types & Featured Images in WordPress RSS Feed

A client of mine needed to display their featured images/post thumbnails in their RSS Feed. Easy enough, Google to the rescue on that. But while WordPress’ built in all around image uploader/resizer/cropper works great, sometimes the thumbnails are a little lame since you don’t have control over how they get cropped. The client therefore occasionally created their own cropped versions of images and, using a custom field, added them to the post. So moreover than what is explained above, I need a WordPress function that could do the following:

  1. Check to the see if the plugin has a specific custom field value, if so, display that.
  2. If not, check to see if the plugin has a post thumbnail, if so, display that.
  3. If neither of the above are true, display some default image.

If you’d like to implement this, it’s not all that hard, and of course I’ll walk you through it.

  1. Firstly, you’ll need to become familiar with WordPress’ custom fields, and with featured images / post thumbnails.
  2. You’ll be using a custom field for your very custom post thumbnail (the one you’ll create for yourself, if you don’t want to rely on WordPress’ cropping abilities). For the sake of this article, we’ll assume your custom field has a key of custom_thumbnail
  3. Next, copy and paste the following code into your theme’s functions.php file. We’ll discuss the details later:function rss_post_thumbnail($content) {

    global $post;

    $cn_image = get_post_meta($post->ID, custom_thumbnail, true);

    if ($cn_image != '') { $customfeederimage = '<img src="' . $cn_image . '" />'; }

    if (has_post_thumbnail($post->ID)) { if ($cn_image == '') { $customfeederimage = get_the_post_thumbnail($post->ID, array(165,165) ); } }

    if (!has_post_thumbnail($post->ID) && $cn_image == '' ) { $customfeederimage = '<img src="/whatever.jpg" />';}

    $content = '<p>' . $customfeederimage . '</p>' . get_the_excerpt();

    return $content;

    }

    add_filter('the_excerpt_rss', 'rss_post_thumbnail');

    add_filter('the_content_feed', 'rss_post_thumbnail');

  4. Save your functions.php file and you’re good to go, after you make any necessary modifications (as outlined below).

Customizing the Code

Here we’ll analyze the important, customizable pieces of the code. In this first area, we’re setting our $cn_image variable to be the value of our custom field with the key custom_thumbnail. If your custom field has a different key, use that instead.

$cn_image = get_post_meta($post->ID, custom_thumbnail, true);

Next we’ll check to see if there was a custom field value. If so, set our $customfeederimage variable to equal an image tag with the src value being our custom image.

if ($cn_image != '') { $customfeederimage = '<img src="' . $cn_image . '" />'; }

Next we’ll say, okay, check to see if the post has a post thumbnail. If so, and if there was no custom field value, make our $customfeederimage equal the post thumbnail. Also note the 165,165. That is where I’m specifically setting the width and height of the thumbnail. You can remove the array(165,165) altogether and use the default ‘thumbnail’ value, or any others from get_the_post_thumbnail.

if (has_post_thumbnail($post->ID)) { if ($cn_image == '') { $customfeederimage = get_the_post_thumbnail($post->ID, array(165,165) ); } }

And finally, if there was no post thumbnail and the custom field was blank, display a default image.

if (!has_post_thumbnail($post->ID) && $cn_image == '' ) { $customfeederimage = '<img src="/whatever.jpg" />';}

Finally, you can switch out get_the_excerpt() with get_the_content() if you’d rather display your feed’s entire content vs. just an excerpt (and I would recommend this!)

$content = '<p>' . $customfeederimage . '</p>' . get_the_excerpt();

People are Talking, Talking 'bout People

  1. Hi There!
    Thanks a lot for sharing this info, quite useful!

    I have a question. I am hosting a photoblog (photos.abhizworld.com) and display images in the post using custom field ‘image’. I used your method, but it does not help. I don’t have any thumnails in the post and I do not insert images,but only display using the ‘image’ variable value.

    -Abhi

  2. Abhi: Where I use the code:
    $cn_image = get_post_meta($post->ID, custom_thumbnail, true);

    You’ll just want to change that to read:
    $cn_image = get_post_meta($post->ID, image, true);

  3. Thanks Nathan! I tried the same. I think for some reason photos.abhizworld.com/feed does not pull up the images. But when I access it via Feedburner it does show the images (http://feeds.feedburner.com/AbhizPhotoBlog) ! Thanks again for making it so simple.

  4. I see them in your feed here: feed://feeds.feedburner.com/AbhizPhotoBlog

    Open that up in Safari and you should, too. What are you using to view the feed?

  5. I tried it on :
    http://simplepie.org/demo/?feed=http%3A%2F%2Fphotos.abhizworld.com%2Ffeed%2F

    It seems to work everywhere now! May be I was too quick to check and re-check.

    Thanks again!
    -Abhi

Your Turn

Next at bat? Well that'd be you! Swing away...

Your email address will not be published.