2

I am looking for a way to use an image url with the WordPress function add_image_size, I have already setup an image size called 'custom-thumb' For example, instead of...

the_post_thumbnail( 'custom-thumb' );

Which would output the posts thumbnail at the custom size I specified, I want to do something like...

www.mydomain.com/image.jpg('custom-thumb');
4
  • add_image_size adds a size for WordPress to use when generating thumbnails. I don't see how using an URL even comes into that. – s_ha_dum Oct 5 '13 at 20:13
  • Correct, add_image_size adds a size for WordPress to use. I now want to output an image at that size by its WordPress URL. Make sense? – fightstarr20 Oct 5 '13 at 20:16
  • No, still doesn't make sense. What does "output an image at that size by its WordPress URL" mean? You want to display an image at that size, but how does the URL get involved? – s_ha_dum Oct 5 '13 at 20:18
  • I have updated the original post to try and better explain the question – fightstarr20 Oct 5 '13 at 20:34
1

The pattern you posted...

www.mydomain.com/image.jpg('custom-thumb');

... is a bit odd. That would be pretty tricky. You'd need a PHP handler to load the image and you'd need to tell the server (Apache, Nginx, IIS, Whatever) to parse that file ending as PHP. Something like this would be simpler:

www.mydomain.com/image.php?size=custom-thumb

You would still need to create a PHP handler script to read the URL, parse the GET string, and display the image.

You could probably get something like this...

www.mydomain.com/image/custom-thumb-X

... working with an endpoint.

However, the easiest thing to do is use wp_get_attachment_image_src to create the URL. I don't know if that is an option for you but there is an example in the Codex:

<?php 
$attachment_id = 8; // attachment ID

$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?> 

<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
1

TimThumb (source code) is probably what you think you need, and probably good enough now, considering that it's been a while since it was last exploited. Like anything else, it comes with its own pros/cons (just google 'em).

But you should know that once you get the hang of add_image_size and the_post_thumbnail functions you'll never have to go back.

PS: Besides it looks like an XY problem. So, considering that you think X is the solution to problem Y, why don't you tell us more about Y, and see if we can suggest a better alternative to X?

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.