1

I need thumbnail image url with size 148*148px. I using custom post type for testimonials. I need just the image source like http://example.com/demoimage.jpg. It is possible to get cropped image source. 'add_image_size' doesn't solve my problem. I have some classes and some data attribute in my <img> tag. Please help Thank You

  • Does your custom post type support thumbnails? If you use add_image_size to define the size of the thumbnail you need, every time you upload a new image it will be cropped/resized to the specified dimensions. You may need to regenerate the thumbnails for images previously uploaded. Force regenerated thumbnails is a good choice to do it. – cybmeta Apr 18 '14 at 8:34
  • hi cybnet Thanks for the information. It worked for me when I regenerated the thumbnails. – Pankaj Apr 18 '14 at 11:43
  • So, the answer you have marked is not the correct answer. You should unmark it to not confuse other people. – cybmeta Apr 18 '14 at 14:28
1

You should add your custom image size with this code:

add_image_size( 'my-test-image-size', 148, 148, true );

Then in your template file you can get cropped image using wp_get_attachment_image_src like this:

$attachment_id = ...;  // i.e. get_post_thumbnail_id()
$image_info = wp_get_attachment_image_src($attachment_id, 'my-test-image-size');
echo '<img src="'. $image_info[0] .'" ... />';  // $image_info[0] contains url of cropped image

To be more precise... wp_get_attachment_image_src returns an array containing:

  • [0] => url
  • [1] => width
  • [2] => height
  • [3] => boolean: true if $url is a resized image, false if it is the original.

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.