最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Add a Post Thumbnail to an RSS Feed with custom size

programmeradmin2浏览0评论

I have two different blogs on the same domain and my goal is to get the 3 latest posts from blog1 to show on the home page of blog2. Everything goes well following these instructions:

The problem comes when I want to style the image, I need them to have all the same size/proportion using the custom sizes in wordpress. To achieve this I use the following code on functions.php in blog1 but I keep getting the huge image, not the small one:

if ( function_exists( 'add_image_size' ) ) add_theme_support( 'post-thumbnails' );

if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'home1-thumb', 630, 320, true );
}


//add post thumbnails to RSS images
function cwc_rss_post_thumbnail($content) {
   global $post;
     if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID,'home1-thumb') .
    '</p>' . get_the_excerpt();
     }
    return $content;
 }
 add_filter('the_excerpt_rss', 'cwc_rss_post_thumbnail');
 add_filter('the_content_feed', 'cwc_rss_post_thumbnail'); 

Edit: In case it's of any help, the feed we are talking about is / It's running a 3.4.2 install as of now (I think it was on the previous version), I don't have much info on the server it's running on (what should I be looking for so I know what to ask?) and the list of plugins is unrelated to the problem but here it is:

Simple google analytics, regenerate thumbnails, google xml sitemaps, advanced custom fields, akismet, newsletter, Responsive Select Menu. Nothing cache related.

I have two different blogs on the same domain and my goal is to get the 3 latest posts from blog1 to show on the home page of blog2. Everything goes well following these instructions: http://www.worldoweb.co.uk/2012/display-wordpress-posts-on-another-wp-blog

The problem comes when I want to style the image, I need them to have all the same size/proportion using the custom sizes in wordpress. To achieve this I use the following code on functions.php in blog1 but I keep getting the huge image, not the small one:

if ( function_exists( 'add_image_size' ) ) add_theme_support( 'post-thumbnails' );

if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'home1-thumb', 630, 320, true );
}


//add post thumbnails to RSS images
function cwc_rss_post_thumbnail($content) {
   global $post;
     if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID,'home1-thumb') .
    '</p>' . get_the_excerpt();
     }
    return $content;
 }
 add_filter('the_excerpt_rss', 'cwc_rss_post_thumbnail');
 add_filter('the_content_feed', 'cwc_rss_post_thumbnail'); 

Edit: In case it's of any help, the feed we are talking about is http://institutoespanol/blog/feed/ It's running a 3.4.2 install as of now (I think it was on the previous version), I don't have much info on the server it's running on (what should I be looking for so I know what to ask?) and the list of plugins is unrelated to the problem but here it is:

Simple google analytics, regenerate thumbnails, google xml sitemaps, advanced custom fields, akismet, newsletter, Responsive Select Menu. Nothing cache related.

Share Improve this question edited Sep 19, 2012 at 13:25 Elaine Marley asked Sep 19, 2012 at 11:41 Elaine MarleyElaine Marley 1031 silver badge9 bronze badges 4
  • try using wordpress/extend/plugins/regenerate-thumbnails to regenerate you thumbnails. – Bainternet Commented Sep 19, 2012 at 11:55
  • yep, already using it, the thumbnails are there but the image being displayed by the feed is the big one (original size) – Elaine Marley Commented Sep 19, 2012 at 11:58
  • Update your questions with more details of your environment, server? WP version? any cache plugins in place? – Bainternet Commented Sep 19, 2012 at 13:13
  • I edited the question as suggested – Elaine Marley Commented Sep 20, 2012 at 9:23
Add a comment  | 

2 Answers 2

Reset to default 0

Well after doing nothing, two days later it seems to be working. My guess is the RSS needed to update the images after changing the code (I did everything, checked if it was working, then tried to add the custom size for the image). Well atleast the code is right in case anyone needs to achieve the same.

While your code is technically correct, there's one small change i'd do, just to prepend the thumbnail to the code. Using your code will break the Full Text / Summary admin setting.

function cwc_rss_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $precontent = '<p>' . get_the_post_thumbnail(get_the_id()) .'</p>';
        $content = $precontent.$content;
    }
    return $content;
}
发布评论

评论列表(0)

  1. 暂无评论