2012
07.03

About 2 years ago I’ve created an autoblog network for the company I work for.
At that time I’ve figured out, that it would be freakin’ nice if we just extend the RSS feed with the first image from the post’s attachments.

Here I’ll publish a simplified version of my original code, thus there were many customizations in that, what a normal user wouldn’t need.

I left there only one thing, which is the width management.

So, here is my solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/*
Plugin Name: Extend feed with image
Plugin URI: http://djzone.im
Description: Extend feed with image
Version: 1.0
Author: DjZoNe
Author URI: http://djzone.im
*/


function extend_my_feed($content)
{
    global $post;
   
    $image_src = '';
   
  $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
  $total_images = count( $images );

    if ($total_images > 0)
    {
        $image = array_shift( $images );
        $image_img_src = wp_get_attachment_image_src( $image->ID, 'large' );
        $meta = wp_get_attachment_metadata( $image->ID);
       
        if(!empty($image_img_src))
        {
            $image_src .='<a target="_blank" href="'.get_permalink().'">';
            $image_src .='<img ';
            if(!empty($meta) && !empty($meta['width']) && $meta['width'] >= 564 )
            {
                $image_src .= 'style="width:564px"';
            }
            $image_src .= 'src="' . $image_img_src[0] . '" alt="'.get_the_title() . '" /></a><br /><p><strong>Click on the image to see more!</strong></p><br />';
        }
    }

    if (!empty($image_src)) $content = $image_src . $content;
   
    return $content;
}

add_filter("the_excerpt_rss",'extend_my_feed');

No Comment.

Add Your Comment