我如何仅在the_post_thumbnail上获得图像URL
-
-
[获取缩略图路径而不是图像标记]的可能重复项(http://wordpress.stackexchange.com/questions/4745/getting-thumbnail-path-rather-than-image-tag)possible duplicate of [Getting Thumbnail Path rather than Image Tag](http://wordpress.stackexchange.com/questions/4745/getting-thumbnail-path-rather-than-image-tag)
- 0
- 2011-02-12
- Jan Fabry
-
6 个回答
- 投票数
-
- 2011-02-12
您也可以尝试:
如果您只有一个尺寸的缩略图:
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
或者...如果您有多种尺码:
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );
请注意,wp_get_attachment_image_src()返回一个数组:url,宽度,高度,is_intermediate.
因此,如果您只想要图片网址:
echo $thumbnail[0];
资源:
You 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:
-
一个小提示:如果您正在使用wp_get_attachment_image_src()函数使用大小,并且想要获取确切的缩略图大小:请使用定义中定义的缩略图名称(函数add_image_size()).如果您使用具有尺寸的数组,WP将使用具有适当宽度或高度的第一个图像尺寸.因此,您可能会得到错误的图像.例如:如果您定义了2张图片:156x98和120x98(高度相同),则可能不是120x98,而是156x98.我爱上了一次;)A little hint: if you are using wp_get_attachment_image_src() function with size and want to get exact thumbnail size: use thumbnail name given in definition (function add_image_size()). If you use array with dimensions WP will use first image size that have proper width or height. So you may get wrong image. Example: instead of 156x98 you might have got 120x98 if you have 2 images defined: 156x98 & 120x98 (height is the same). I fell for it once ;)
- 0
- 2011-10-16
- Marek Tuchalski
-
-
我不知道自2012年以来情况是否有所改变,但是在2017年," wp_get_attachment_image_src"的第一个参数必须是附件ID号,而不是大小.I don't know if this has changed since 2012, but in 2017 the first parameter of `wp_get_attachment_image_src` must be the attachement id number, not the size.
- 1
- 2017-05-11
- squarecandy
-
-
- 2017-09-15
自WordPress 4.4起,有一个有效的核心功能可以比此处的答案更简洁地处理此问题.
您可以使用
the_post_thumbnail_url( $size )
打印帖子缩略图的网址.或者,如果要返回URL而不是立即输出URL,则可以使用
$url = get_the_post_thumbnail_url( $post_id, $size )
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 )
-
- 2011-02-12
$dom = simplexml_load_string(get_the_post_thumbnail()); $src = $dom->attributes()->src; echo $src;
欢迎使用另一种方法.
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.
-
- 2018-10-26
请使用以下代码
<?php get_the_post_thumbnail_url(); ?>
如果这还不足以实现您的目标,请尝试使用以下代码
<?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 ); ?>
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 ); ?>
-
- 2019-02-17
快速&amp;肮脏的解决方案,将其拍在您主题的functions.php文件中
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); }
在循环中使用,这将为您提供所需的内容
这将返回类似
http://foo.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="');
* "在循环中"=寻找类似while(have_posts()):the_post();
** 您还可以使用以下任何预定义的图像尺寸来替换" 后放大": 缩略图后, 后中 后满
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://foo.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" = 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
-
那很糟.为什么要使用全部大写的代码?that's bad. why do you use all caps for your codes?
- 0
- 2020-06-03
- Raptor
我想知道如何在
上获取图片网址the_post_thumbnail()
默认
the_post_thumbnail()
在这里,我只想获取src.如何仅过滤
the_post_thumbnail()
以获得http://domain.com/wp-content/uploads/2011/02/book06.jpg
让我知道