Image Wordpress获取图像url(当前使用wp\u获取\u附件\u url)
在wordpress中获取图像附件URL有点困难。这是我目前的代码:Image Wordpress获取图像url(当前使用wp\u获取\u附件\u url),image,wordpress,url,attachment,Image,Wordpress,Url,Attachment,在wordpress中获取图像附件URL有点困难。这是我目前的代码: <?php // find images attached to this post / page. global $post; $thumb_ID = get_post_thumbnail_id( $post->ID ); $args = array( 'post_type' => 'attachment
<?php // find images attached to this post / page.
global $post;
$thumb_ID = get_post_thumbnail_id( $post->ID );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'exclude' => $thumb_ID,
'post_parent' => $post->ID
);
$images = get_posts($args);
?>
<?php // Loop through the images and show them
if($images)
{
foreach($images as $image)
{
echo wp_get_attachment_image_url($image->ID, $size='attached-image');
}
}
?>
它什么也不返回。如果我调出
wp\u获取附件\u图像\u url($image->ID,$size='attached-image')代码>用于wp\u获取附件\u图像($image->ID,$size='attached-image')代码>这可以正常工作,但会引入图像而不仅仅是url。以下代码将遍历所有图像附件并输出src url。请注意,根据您的需要,显示了两种方法
<?php
global $post;
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
// Method #1: Allows you to define the image size
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {echo $src[0];}
// Method #2: Would always return the "attached-image" size
echo $attachment->guid;
}
}
?>
我相信您正在寻找wp\u get\u attachment\u url($id)代码>或wp\u get\u attachment\u image\u src($attachment\u id,$size,$icon)代码>相反-您引用的函数不是WP函数。对不起,我对php相当陌生。我如何使用上述代码循环浏览所有图片并输出url?在阅读了您的评论和问题之后,我意识到可能还有其他问题:您想要所有附加的图片,还是只想要特色图片?您的代码get\u post\u缩略图\u id
仅获取特色图像。你的评论表明你想看到这篇文章的所有图片-是吗?是的,我正在尝试获取附加图片的URL,不包括特色图片。在发布之后,我使用$attachment\u id=$image->id;//附件ID$image_attributes=wp_get_attachment_image_src($attachment_ID,'full',false);//返回数组回显“”代码>谢谢!我使用wp_get_attachment_src实现了类似的功能,但方式略有不同。你的路更干净,所以谢谢你!附件的GUID属性只是一个GUID——它不应该被视为URL,可以在任何地方使用。最后一行“echo$attachment->guid”不能显示实际不是真实URL的URL。看见