admin管理员组文章数量:1200385
I want to know how to get just the image URL from the_post_thumbnail()
. The default output of the_post_thumbnail()
is:
<img width="800" height="533" src=".jpg" class="attachment-post-thumbnail wp-post-image" alt="book06" title="book06" />
Here I want grab the src
only. How do I filter the_post_thumbnail()
to get only .jpg
?
I want to know how to get just the image URL from the_post_thumbnail()
. The default output of the_post_thumbnail()
is:
<img width="800" height="533" src="http://example.com/wp-content/uploads/2011/02/book06.jpg" class="attachment-post-thumbnail wp-post-image" alt="book06" title="book06" />
Here I want grab the src
only. How do I filter the_post_thumbnail()
to get only http://example.com/wp-content/uploads/2011/02/book06.jpg
?
- possible duplicate of Getting Thumbnail Path rather than Image Tag – Jan Fabry Commented Feb 12, 2011 at 15:55
6 Answers
Reset to default 42You might also try:
If you only have one size thumbnail:
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
Or...if you have multiple sizes:
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );
Note that wp_get_attachment_image_src() returns an array: url, width, height, is_intermediate.
So if you just want only the image url:
echo $thumbnail[0];
Resources:
- http://wpcanyon.com/tipsandtricks/get-the-src-attribute-from-wordpress-post-thumbnail/
- https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
Since WordPress 4.4, there's an efficient core function that can handle this in a cleaner way than the answers here.
You can use the_post_thumbnail_url( $size )
which will print the URL of the post thumbnail.
Alternatively if you want to return the URL instead of immediately output it, you can use $url = get_the_post_thumbnail_url( $post_id, $size )
This does the trick:
<?php wp_get_attachment_image_src('subgall-thumb'); ?>
Make sure you use the correct name for the thumbnail that you are calling.
Ok got it using simplexml_load_string
$dom = simplexml_load_string(get_the_post_thumbnail());
$src = $dom->attributes()->src;
echo $src;
Another method are welcome.
Please Use the below code
<?php get_the_post_thumbnail_url(); ?>
If It's not enough to achieve your goal then try below code
<?php $postimages = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
// Check for images if ( $postimages ) {
// Get featured image $postimage = $postimages[0];
} else {} while (have_posts() && $i < 8) : the_post(); echo esc_url( $postimage ); ?>
For a quick & dirty solution, slap this in the functions.php
file of your theme
FUNCTION GET_STRING_BETWEEN($STRING, $START, $END){
$STRING = " ".$STRING;
$INI = STRPOS($STRING, $START);
IF ($INI == 0) RETURN "";
$INI += STRLEN($START);
$LEN = STRPOS($STRING, $END, $INI) - $INI;
RETURN SUBSTR($STRING, $INI, $LEN);
}
Used within the loop, this will give you what you're looking for
This will return something like http://example.com/wp-content/uploads/2019/02/toy-story-two-was-ok.jpg
$THE_FEATURED_IMAGE = GET_STRING_BETWEEN(get_the_post_thumbnail(NULL,'post-large'), 'src="', '" class="');
"Within the loop" means look for something like while ( have_posts() ): the_post();
.
You can also sub out post-large
with any of these predefined image sizes:
post-thumbnail
post-medium
post-full
本文标签: post thumbnailsHow do I get only the image URL from thepostthumbnail()
版权声明:本文标题:post thumbnails - How do I get only the image URL from the_post_thumbnail()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738526887a2092842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论