1

I have this function:

function get_image_link( &$post ) {
         $image_link_meta = get_post_meta( $post->ID, 'as_link_to_image', true ); 

         $image_link_from_post = '';

         if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $post->ID ) ) {
            $attachment_image_link = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), full );
         }

         if ( $attachment_image_link[0] )
            $image_link_from_post = $attachment_image_link[0];

         if( !empty( $image_link_meta ) ) 
            $final_image_link = $image_link_meta;

         elseif( !empty( $image_link_from_post ) )
            $final_image_link = $image_link_from_post; 

         else
            $final_image_link = WP_PLUGIN_URL . '/link/to/no-image.png';

         return $final_image_link;
    }
}

Any idea why $image_link_from_post isn't getting a value?

1
  • 1
    Cannot reproduce. Aside from missing quotes around full and extra bracket at the end this seems to be working.
    – Rarst
    Feb 1, 2011 at 6:41

3 Answers 3

2

This should be able to return the url of the large image.

$largeImg = wp_get_attachment_link( $attachment_id, 'large', false );

There's also this incase that doesn't work.

wp_get_attachment_image_src( $attachment_id, 'large' );
2

Great Snippet available here from the guys at CSS-Tricks:

$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];

When I first tried this solution it kept throwing 'Missing Attachment' because I was referencing the post ID instead of the attachment ID. Anywho this little snippet helped clear that up for me. First comment reduces it to one line!

Cheers!

0

This line right here:

if ( !$attachment_image_link[0] )
            $image_link_from_post = $attachment_image_link[0];

You're only assigning the $image_link_from_post variable to the post thumbnail if the post thumbnail does not exist. Take out the NOT operator in the conditional and it should work more the way you intend.

Update: oops, weird, I just copied that line out of your code, but now its not there. Did you already solve the problem?

5
  • corrected it.. it was a typo :-)
    – Joann
    Feb 1, 2011 at 0:01
  • Does the function work now for you? Feb 1, 2011 at 0:01
  • No, unfortunately..
    – Joann
    Feb 1, 2011 at 0:05
  • In that case I'm not sure. You should quote 'full' in the call to get_post_thumbnail_id. Also, check for current_theme_supports( 'post-thumbnails' ) rather than function_exists( 'has_post_thumbnail' ), that's all I can offer. Feb 1, 2011 at 0:17
  • I added support to my theme already particularly in functions.php. Thanks for the time!
    – Joann
    Feb 1, 2011 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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