php 在wordpress中使用wp_get_attachment_image()的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3089624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way of using wp_get_attachment_image() in wordpress
提问by Constant Meiring
I'm looking for the correct way of using wp_get_attachment_image().
我正在寻找使用 wp_get_attachment_image() 的正确方法。
The following code:
以下代码:
<?php
$args = array(
'type' => 'attachment',
'category_name' => 'portfolio'
);
$attachments = get_posts($args);
print_r($attachments);
?>
Generates the following result:
生成以下结果:
Array
(
[0] => stdClass Object
(
[ID] => 54
[post_author] => 1
[post_date] => 2010-06-22 00:32:46
[post_date_gmt] => 2010-06-22 00:32:46
[post_content] => <a href="http://localhost/wordpress/wp-content/uploads/2010/06/Capture.jpg"><img class="alignnone size-medium wp-image-55" title="Capture" src="http://localhost/wordpress/wp-content/uploads/2010/06/Capture-300x114.jpg" alt="" width="300" height="114" /></a>
[post_title] => Our Own Site
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => our-own-site
[to_ping] =>
[pinged] =>
[post_modified] => 2010-06-22 00:40:22
[post_modified_gmt] => 2010-06-22 00:40:22
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost/wordpress/?p=54
[menu_order] => 0
[post_type] => post
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
The following, however, doesn't return anything.
但是,以下不会返回任何内容。
<?php
echo wp_get_attachment_image(54, array('300', '300'));
?>
What am I doing wrong here?
我在这里做错了什么?
采纳答案by Aaron Harun
The function wp_get_attachment_imageonly gets an image that was uploaded to wordpress, it doesn't output an image in the content of the post.
该函数wp_get_attachment_image仅获取上传到 wordpress 的图像,不会在帖子内容中输出图像。
You have to output the content of the post for your example image.
您必须为示例图像输出帖子的内容。
Like: echo $attachments['post_content'];
喜欢: echo $attachments['post_content'];
回答by rinogo
Actually, I don't think the accepted answer really answers the question.
实际上,我认为接受的答案并不能真正回答问题。
Your problem is that you're passing in the post id(54in your example; typically $post->IDin WP parlance) to wp_get_attachment_image(). As can be seen in the codex, you're supposed to use the attachment id(see $attachment_idbelow):
您的问题是您将帖子 ID(54在您的示例中;通常$post->ID用 WP 用语)传递给wp_get_attachment_image(). 正如在codex 中所见,您应该使用附件 ID(见$attachment_id下文):
wp_get_attachment_image( $attachment_id, $size, $icon );
In other words, you've got to do something like this:
换句话说,你必须做这样的事情:
$image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
回答by Rinku
wp_get_attachment_image function can accept four values as you can see:
wp_get_attachment_image 函数可以接受四个值,如您所见:
wp_get_attachment_image ( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )
So i always use:
所以我总是使用:
<?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) ); ?>
Note: we can simply use get_the_ID()to pass id of active post. and here 700 is widthand 600 is heightof attachment image. we can also pass our class as array( "class" => "img-responsive" )
注意:我们可以简单地使用get_the_ID()传递活动帖子的 id。这里700 是宽度,600 是附件图像的高度。我们也可以将我们的类作为数组传递(“class”=>“img-responsive”)

