1

In a post I want to place a custom image. how do i place a thumbnail with link to original image? below is my current code.

<a href="[site_url]/wp-content/uploads/GABON-Leopard-Gas-Discovery-1x1.jpg"><img class="alignright size-medium wp-image-5435" src="[site_url]/wp-content/uploads/GABON-Leopard-Gas-Discovery-1x1-200x112.jpg" alt="GABON-Leopard-Gas-Discovery-1x1" width="288" height="288" /></a>

I had to manually put in -200x112 . is there any easier way to do this?

  • 1
    You can select image/thumbnail size from Attachment Display Settings (right column) in media upload popup. – Robert hue Dec 9 '14 at 13:06
  • im not using visual editor, as it messes up layout of post. – shorif2000 Dec 9 '14 at 14:10
4

If you know the image's ID :

wp_get_attachment_image( $image->ID, array(200, 112) );

returns an HTML img element or empty string on failure. The entire code could be something like :

<a href="<?= get_attachment_link( '5435' ); ?>">
   <?= wp_get_attachment_image( '5435', array(200, 112) ); ?>
</a>
1

By using wp_get_attachment_image_src() function get the original url of thumbnail

if ( has_post_thumbnail() ) {
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$image = $image_url[0];

}

Then add Image Url to your custom code

<a href="<?php echo $image; ?>"><img class="alignright size-medium wp-image-5435" src="<?php echo $image; ?>" alt="GABON-Leopard-Gas-Discovery-1x1" width="288" height="288" /></a>

I think it will work fine

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.