WordPress cropped thumbnail for RSS feed

I’m using the following to generate a thumbnail image in WordPress for an RSS feed:

<media:content medium="image" width="128" url="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>" />

How can I replace that image with a resized & cropped thumbnail square at 128x128px?

Related posts

Leave a Reply

1 comment

  1. Use wp_get_attachment_image_src instead.

    Obviously you would want to add some error checking, but this would be sample usage given your scenario:

    // wp_get_attachment_image_src requires the attachment id, so get that first
    $attachment_id = get_post_thumbnail_id($post->ID);
    // define the desired size as an array in the second option
    $image_attr = wp_get_attachment_image_src($attachment_id, array(128,128));
    // wp_get_attachment_image_src returns an array: 
    // [0] = url, [1] = width, [2] = height, [3] = resized (true / false)
    // Retrieve the url from the array
    $url = $image_attr[0];
    
    // pass the url into your media element
    <media:content medium="image" width="128" url="<?php echo $url; ?>" />