获取缩略图路径而不是图像标签
4 个回答
- 投票数
-
- 2010-12-01
缩略图本质上是附件,因此您可以从那一侧入手-使用
get_post_thumbnail_id()
并使用wp_get_attachment_image_src()
获取数据,就像这样:if (has_post_thumbnail()) { $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name'); echo $thumb[0]; // thumbnail url }
(源)
Thumbnail is essentially attachment so you can approach from that side - lookup ID with
get_post_thumbnail_id()
and fetch data withwp_get_attachment_image_src()
, like this:if (has_post_thumbnail()) { $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name'); echo $thumb[0]; // thumbnail url }
(source)
-
- 2016-12-21
get_the_post_thumbnail_url($recent['ID']);
以上内容对我有用!我不得不猜测该功能,它神奇地起作用了!
值得一提的是,我在此过程中使用了
get_recent_posts
循环.get_the_post_thumbnail_url($recent['ID']);
The above did the trick for me! I had to guess the function and it magically worked!
Its is good to mention that I used
get_recent_posts
loop in the process. -
- 2010-12-01
一种方法是将从
get_the_post_thumbnail()
返回的所有内容转换为对象,并提取src
属性:$thumbnail = new SimpleXMLElement( get_the_post_thumbnail( $postid ) ); print $thumbnail->attributes()->src;
One method would be to convert whatever is returned from
get_the_post_thumbnail()
to an object, and pull thesrc
attribute:$thumbnail = new SimpleXMLElement( get_the_post_thumbnail( $postid ) ); print $thumbnail->attributes()->src;
-
- 2010-12-01
当我需要显示附加到帖子库的缩略图时,我在我的functions.php中使用了自定义函数.满足您的需要可能会过分杀人,但它应该涵盖所有内容.
在此示例中,我检索了帖子库中的所有图像,然后在列表项中显示每个图像.该列表包含包裹在锚点中的缩略图,该锚点链接到该图像的来源.输出字符串可以轻松地根据您的需求进行定制.
function get_gallery_image_thumb_list($size){ global $post; $args = array( 'numberposts' => null, 'post_parent' => $post->ID, 'post_type' => 'attachment', 'nopaging' => false, 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'post_status' => 'any' ); $attachments =& get_children($args); if ($attachments) { foreach($attachments as $attachment) { foreach($attachment as $attachment_key => $attachment_value) { $imageID = $attachment->ID; $imageTitle = $attachment->post_title; $imageCaption = $attachment->post_excerpt; $imageDescription = $attachment->post_content; $imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true); $imageArray = wp_get_attachment_image_src($attachment_value, $size, false); $imageURI = $imageArray[0]; // 0 is the URI $imageWidth = $imageArray[1]; // 1 is the width $imageHeight = $imageArray[2]; // 2 is the height // Build the <img> string $ImgString = '<li><a href="' . get_permalink() . '" title="' . the_title("", "", false) . '"><img src="' . $imageURI . '" width="' . $imageWidth . '" height="' . $imageHeight . '" alt="' . $imageAlt . '" title="' . $imageTitle . '" /></a></li>'; // Print the image echo $ImgString; break; } } } unset($args);}
然后调用该函数并传入您想要返回的图像大小(缩略图,中,大或完整),如下所示:
get_gallery_image_thumb_list("thumbnail");
这需要在"循环"或自定义循环中调用.
When I need to display a thumbnail that is attached to a post gallery, I use a custom function in my functions.php. It might be over kill for your needs, but it should cover everything.
In this example, I retrieve all the images in a post's gallery, and then display each image in a list item. The list contains the thumbnail image wrapped in an anchor that links to the post the image came from. The output string can easily be customized to your needs.
function get_gallery_image_thumb_list($size){ global $post; $args = array( 'numberposts' => null, 'post_parent' => $post->ID, 'post_type' => 'attachment', 'nopaging' => false, 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'post_status' => 'any' ); $attachments =& get_children($args); if ($attachments) { foreach($attachments as $attachment) { foreach($attachment as $attachment_key => $attachment_value) { $imageID = $attachment->ID; $imageTitle = $attachment->post_title; $imageCaption = $attachment->post_excerpt; $imageDescription = $attachment->post_content; $imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true); $imageArray = wp_get_attachment_image_src($attachment_value, $size, false); $imageURI = $imageArray[0]; // 0 is the URI $imageWidth = $imageArray[1]; // 1 is the width $imageHeight = $imageArray[2]; // 2 is the height // Build the <img> string $ImgString = '<li><a href="' . get_permalink() . '" title="' . the_title("", "", false) . '"><img src="' . $imageURI . '" width="' . $imageWidth . '" height="' . $imageHeight . '" alt="' . $imageAlt . '" title="' . $imageTitle . '" /></a></li>'; // Print the image echo $ImgString; break; } } } unset($args);}
Then call the function and pass in the size of image you want returned (thumbnail, medium, large or full) like so:
get_gallery_image_thumb_list("thumbnail");
This will need to be called in The Loop or a custom loop.
我看到了许多在WordPress中显示缩略图的方法,但是我不确定如何仅获取帖子缩略图的路径,而不是由诸如
the_post_thumbnail()
之类的函数生成的HTML就绪代码.和get_the_post_thumbnail()
.我可以使用哪些方法仅获取缩略图的路径(将其设置为bgimage)而不是获取 标记的缩略图?我只能选择解析
get _
方法的结果还是有更简单的方法?