1

I use this plugin to echo the URL of a post's featured image in the header:

<?php
/** Plugin Name: Post Thumbnail FB header */
function fb_header()
{
    // Not on a single page or post? Stop here.
    if ( ! is_singular() )
        return;

    $post_ID = get_queried_object_id();

    // We got no thumbnail? Stop here.
    if ( ! has_post_thumbnail( $post_ID ) )
        return;

    // Get the Attachment ID
    $att_ID = get_post_thumbnail_id( $post_ID );

    // Get the Attachment
    $att    = wp_get_attachment_image_src( $att_ID );

    printf(
         '<link rel="image_src" href="%s" />'
        ,array_shift( $att )
    );
}
add_action( 'wp_head', 'fb_header' );
?>

As it stands currently, it echo's the featured image. I want it to echo the URL of the Large version of the featured image. How can I do this? Just to note that all my featured images have a large version...

2

Use the second parameter of wp_get_attachment_image_src(): $size.

$att    = wp_get_attachment_image_src( $att_ID, 'large-thumb' );

or

$att    = wp_get_attachment_image_src( $att_ID, array ( 900, 300 ) );

The size is passed to image_downsize() and there to image_get_intermediate_size(). If $size is an array WordPress will search for the best match in existing images:

// from wp-includes/media.php::image_get_intermediate_size()
// get the best one for a specified set of dimensions
if ( is_array($size) && !empty($imagedata['sizes']) ) {
    foreach ( $imagedata['sizes'] as $_size => $data ) {
        // already cropped to width or height; so use this size
        if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
            $file = $data['file'];
            list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
            return compact( 'file', 'width', 'height' );
        }
  • Can you please elaborate on the 900, 300? Does it search for those dimensions? – Amanda Bynes Oct 31 '12 at 18:05
  • @AmandaBynes See my update. – fuxia Oct 31 '12 at 18:09
  • @torscho I tested your first code, adding large-thumb to the line and it did not work. It returned the full image and not the large thumb. Is there any way you can confirm that it works for you? – Amanda Bynes Oct 31 '12 at 18:18
  • large-thumb was just an example. You have to add the image size in your theme first. See also add-image-size. – fuxia Oct 31 '12 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.