WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Thumbnails/Featured Images in RSS Feed in 3.0 (15 posts)

  1. paulw1
    Member
    Posted 4 years ago #

    Hi there,

    I need to add the Featured Images for each post into my RSS feed, preferably as a new node (something like 'thumbnail') with just the image url.

    I've seen multiple solutions to this for wordpress 2.9 (e.g. this) but have yet to find a solution that works in 3.0. I'm not sure why those solutions no longer work, but if anyone has any ideas they would be greatly appreciated.

    Thanks in advance

  2. paulw1
    Member
    Posted 4 years ago #

    Managed to solve this myself by hacking around with feed-rss2.php under /wp-includes/ (in WordPress 3.0), thought I'd post it here in case it's of use to anyone.

    I ended up using a traditional custom field instead of the Featured Image so that I could simply access an image URL.

    I added the below code to the rss file as a new node under <item>:

    <?php
      $key="project-thumb";
      if ( get_post_meta($post->ID, $key, true) ): ?>
        <thumbnail><?php echo get_post_meta($post->ID, $key, true); ?></thumbnail>
      <?php endif;
    ?>

    in this case my custom field is 'project-thumb', the code checks whether the field has a value or not and if it does it adds the image url (the content of the custom field) as a new node thumbnail.

    Hope this helps some people, had me stumped for a while

  3. mhains
    Member
    Posted 4 years ago #

    For ease-of-use for non-technically inclined contributors, I'd prefer to not use a custom field.

    Any further thoughts on how to call the Featured Image itself?

  4. Phil
    Member
    Posted 4 years ago #

    I'd like to know this also.

    not just in the main feed (domain.com/feed)

    ... but also in the category feeds (domain.com/category/feed)

    Any ideas anyone?

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 4 years ago #

    // THIS INCLUDES THE THUMBNAIL IN OUR RSS FEED
    function insertThumbnailRSS($content) {
    global $post;
    if ( has_post_thumbnail( $post->ID ) ){
    $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '' . $content;
    }
    return $content;
    }
    
    add_filter('the_excerpt_rss', 'insertThumbnailRSS');
    add_filter('the_content_feed', 'insertThumbnailRSS');

    This works fine for me on 3.0.1 to include thumbnail/featured image in RSS feed

  6. Phil
    Member
    Posted 4 years ago #

    Hi Rev.

    This works fine for the main RSS, but not for the category feeds. No images appear in any of those.

  7. rizzus
    Member
    Posted 4 years ago #

    Hi guys, I have the solution for your problems.

    This is the code for the RSS files that you use, in my case "/ wp-includes/feed-rss2.php"

    search in file "<? php the_excerpt_rss ()?>" and add the code just before: "<? php echo '<img src="'.get_first_image().'" height="150" alt="" />' ;?> ". This should be done for every "<? php the_excerpt_rss ()?>" in the file.

    In the event that your theme hasn't the function of seeking the first image of each post is also modified another file, "/ wp-includes/functions.php" which must be added immediately: "get_first_image function () (
    global $ post, $ posts;
    $ First_img ='';
    $ Output = preg_match_all ('/ <img.+src=[\'"]([^\'"]+)[\'"].*> s', $ post-> post_content, $ matches);
    First_img $ = $ matches [1] [0];
    return $ first_img;
    )

    Omit the commas of course. Ps "The feed is not updated immediately, try to see the feed of a category and see the result"

    Sorry for my english ;=)

  8. hazymat
    Member
    Posted 3 years ago #

    Rizzus

    It's not a problem with your English, but with your coding...

    How many times have you seen a function definition start like this:

    get_first_image function ()

    ?

    Or used standard brackets to encapsulate the function instead of curly brackets?

    Major eyeroll.

  9. thedonedeal
    Member
    Posted 3 years ago #

    Here is what I have. I made a few changes since I wanted the image address to appear as a separate node:

    First, in /wp-includes/functions.php, add this to the bottom BEFORE the ending ?>

    function get_first_image() {
    	global $post, $posts;
    
    	$result = preg_split("/\"/", get_the_post_thumbnail( $post->ID, 'thumbnail' ));
    	return $result[1];
    }

    Then in /wp-includes/feed-rss2.php, add the following anywhere within the <item>...</item> area. For example, I added it below the <comments>...</comments>:

    <?php if (get_the_post_thumbnail( $post->ID, 'thumbnail' ) != '') { ?>
    		<featured-image><?php echo new_first_image(); ?></featured-image>
    <?php } ?>

    This will cause a new node to be added to the RSS tree. Hope this works.

  10. rhythmdoctor
    Member
    Posted 3 years ago #

    @Rev. Voodoo - Your solution works great. If I wanted to align the image right or left, IE - add a style to it, how would I do that? Thanks so much for the help

  11. rhythmdoctor
    Member
    Posted 3 years ago #

    Never mind, I got it.

    Add this to your functions.php

    // THIS INCLUDES THE THUMBNAIL IN OUR RSS FEED
    function insertThumbnailRSS($content) {
    global $post;
    if ( has_post_thumbnail( $post->ID ) ){
    $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'alt' => get_the_title(), 'title' => get_the_title(), 'style' => 'float:right;' ) ) . '' . $content;
    }
    return $content;
    }
    add_filter('the_excerpt_rss', 'insertThumbnailRSS');
    add_filter('the_content_feed', 'insertThumbnailRSS');
  12. DannyITR
    Member
    Posted 3 years ago #

    None of the above options puts anything in my RSS feed. Putting Voodoo's function in my functions.php shuts down my site and gives me a white page. The others simply don't do anything at all for me. I have WordPress 3.0.4

    I would never have though putting images in an RSS feed would be sooooo difficult. The only plugin on the net right now that does anything is this one by Kafkaesqui:

    http://wordpress.org/support/topic/line-breaks-in-rss-feed#post-356315

    and even that one just puts the url to the image in the feed and not the image. How can I get this to work?

  13. DannyITR
    Member
    Posted 3 years ago #

    I wonder if it's because I use a plugin that is expired to obtain my custom field images.

    http://scribu.net/wordpress/custom-field-images

  14. DannyITR
    Member
    Posted 3 years ago #

    Found the problem. I was putting the code into includes/functions.php but it has to go in the functions.php found in your theme's directory. A slight detail that was overlooked.

  15. Zane Matthew
    Member
    Posted 3 years ago #

    Just came across this, ran into a similar situation and wanted to say, "Rev. Voodoo" has the optimal solution.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.