#11571 closed enhancement (duplicate)
Provide easy way to return url of thumbnail image
Reported by: | braindrain | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 2.9 |
Component: | Upload | Keywords: | has-patch needs-testing gsoc |
Focuses: | Cc: |
Description
I'm sure that there is probably a hack for doing this, but for those who aren't interested in custom functions, etc., I would like a simple built-in function for referencing the thumbnail image url (or a size version of it) without bringing in the predefined image tag. I tried applying a workaround, but it collided with a plug-in I was trying to use, and hacking a pre-2.9 plug-in to use the post plugin has not gone so well because of the image tag that the get_post_thumbnail function returns.
Attachments (6)
Change History (33)
#2
@
14 years ago
I think the issue is that while there is a get_the_post_thumbnail() function, which corresponds to wp_get_attachment_image(), there is no get_the_post_thumbnail_src(). There is however a wp_get_attachment_image_src() function.
There probably should be a get_the_post_thumbnail_src() -- then again, there is no direct echo for wp_get_attachment_image(), while there is the_post_thumbnail().
As an alternative, you can use wp_get_attachment_image_src() with the right parameters -- check out how get_the_post_thumbnail() calls wp_get_attachment_image() for that.
#4
@
14 years ago
- Cc mikeschinkel@… added
Would this work?
function get_post_thumbnail_src( $post_id, $size = 'thumbnail', $icon = false) { if (!has_post_thumbnail( $post_id )) { $src = false; } else { $size = apply_filters( 'post_thumbnail_size', $size, $post_id, $icon ); $post_thumbnail_id = get_post_thumbnail_id( $post_id ); list($src) = wp_get_attachment_image_src($post_thumbnail_id, $size, $icon); } return apply_filters( 'post_thumbnail_src', $src, $post_id, $post_thumbnail_id, $size, $icon ); }
#5
@
14 years ago
Here's what I ended up doing to do this based on get_the_post_thumbnail()
:
function get_post_thumbnail_url( $post_id = NULL ) { global $id; $post_id = ( NULL === $post_id ) ? $id : $post_id; $post_thumbnail_id = get_post_thumbnail_id( $post_id ); $url = false; if ( $post_thumbnail_id ) { $size = apply_filters( 'post_thumbnail_size', 'post-thumbnail' ); $image = wp_get_attachment_image_src( $post_thumbnail_id, $size, false ); if ( $image ) { list( $src, $width, $height ) = $image; $url = $src; } } return $url; }
If this looks good to add to post-thumbnail-template.php
let me know and I'll submit a patch.
#6
@
14 years ago
- Cc wojtek.szkutnik@… added
- Keywords has-patch needs-testing gsoc added; reporter-feedback removed
#7
@
13 years ago
- Milestone changed from Awaiting Triage to 3.1
- Type changed from feature request to enhancement
#8
@
13 years ago
- Keywords 3.2-early added
- Milestone changed from 3.1 to Future Release
We are past freeze, punting to be reconsidered for 3.2. Marking 3.2-early.
#9
@
13 years ago
I cleaned up the patch.
Not sure about the 'post_thumbnail_src' filter.
If it's introduced, it should be applied everywhere, not just when calling get_the_post_thumbnail_src().
#10
@
13 years ago
Hi - I just came to add this myself as a fresh bug, then found this. I've added a .3 patch which is mostly the same as .2, but with the following changes:
- Removed the filter as I agree with scribu that it doesn't make sense just being added here.
- This also removes the reference to $icon which was undefined
- Updated to call update_post_thumbnail_cache() as per get_the_post_thumbnail()
#11
@
13 years ago
+1 For @leewillis77's patch. Tested in the loop with image size and without image size.
I am wondering if we should also have the companion function for output in the loop, or if that's overkill:
/** * Display The Post Thumbnail Source * * * @param int $size Optional. Image size. Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_src( $width, $height, $crop_flag );. * @param string|array $attr Optional. Query string or array of attributes. */ function the_post_thumbnail_src( $size = 'post-thumbnail', $attr = '' ) { echo get_the_post_thumbnail_src( null, $size, $attr ); }
BTW: My use case is for a responsive portfolio layout. If images have height/width attributes they scale badly with min-width:100% in CSS.
#12
@
13 years ago
A few things to tweak for commit:
No need for the logic on line 112. get_post_thumbnail_id() can take the default null. Likewise we can remove that code from get_the_post_thumbnail().
Docblock needs @return string|bool Image src, or false if the post does not have a thumbnail
.
We can bail with false, rather than the if/else, then the code can become un-indented a bit.
I question the lack of a need for filters. Seems to me like we may even want to duplicate the begin/end_fetch_post_thumbnail_html hooks.
#13
@
13 years ago
I question the lack of a need for filters. Seems to me like we may even want to duplicate the begin/end_fetch_post_thumbnail_html hooks.
In that case, all post thumbnail functions should use get_the_post_thumbnail_src(), so that these new filters are applied consistently.
#14
follow-up:
↓ 16
@
13 years ago
Revised patch (11571.4.diff) which addresses most of nacin's comments. Also fixes a comment in get_the_post_thumbnail.
Happy to add the begin/end_fetch_post_thumbnail_src hooks, but not sure what the use case would be?
Not sure I know enough about the core code to pick up scribu's comments - but happy to have a bash over the next few days if that's helpful?
#16
in reply to:
↑ 14
@
13 years ago
Replying to leewillis77:
Happy to add the begin/end_fetch_post_thumbnail_src hooks, but not sure what the use case would be?
Not sure I know enough about the core code to pick up scribu's comments - but happy to have a bash over the next few days if that's helpful?
After talking with nacin on IRC, the idea is to use the '{begin|end}_fetch_post_thumbnail_html' hooks again.
That way, no other changes are necessary, either on our part or on plugin authors' part.
#19
@
12 years ago
- Milestone changed from 3.3 to Future Release
Too late for new APIs at this point, especially when there's a basic simple workaround: wp_get_attachment_image_src( get_post_thumbnail_id(), 'post-thumbnail' )
.
#22
@
12 years ago
- Keywords 3.4-early removed
- Milestone changed from 3.4 to Future Release
Let's punt this once more. Again, simple workaround exists.
I noticed that get_the_post_thumbnail_src() would return the source, while the corresponding function wp_get_attachment_image_src() returns an array. This isn't very intuitive.
#23
@
10 years ago
get_the_post_thumbnail_src() is a convenience helper where you just want the image URL. I'd agree it's inconsistent with wp_get_attachment_image_src() - however making it consistent would lose its simplicity.
The workaround suggested is actually:
$attachment_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'post-thumbnail' ); $src = $attachment_src[0];
compared to:
$attachment_src = get_the_post_thumbnail_src();
I think there's value in the smaller convenience function over the inconsistency.
This ticket has been around for a while. Can it get in 3.9 early - if not, and there's no desire to add it - can we / should we close?
#24
in reply to:
↑ description
@
9 years ago
- Component changed from Post Thumbnails to Upload
Replying to braindrain:
I'm sure that there is probably a hack for doing this, but for those who aren't interested in custom functions, etc., I would like a simple built-in function for referencing the thumbnail image url (or a size version of it) without bringing in the predefined image tag. I tried applying a workaround, but it collided with a plug-in I was trying to use, and hacking a pre-2.9 plug-in to use the post plugin has not gone so well because of the image tag that the get_post_thumbnail function returns.
#25
@
9 years ago
Looks like this was resolved in:
https://core.trac.wordpress.org/ticket/33070
Good to close?
Can you please add the code of your try and provide some more information about what you tried to achieve?