Php 在wordpress中使用wp_get_attachment_image()的正确方法
我正在寻找使用wp\u get\u attachment\u image()的正确方法 以下代码:Php 在wordpress中使用wp_get_attachment_image()的正确方法,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我正在寻找使用wp\u get\u attachment\u image()的正确方法 以下代码: <?php $args = array( 'type' => 'attachment', 'category_name' => 'portfolio' ); $attachments = get_posts($args); print_r($attachments); ?> 生成以下结果: Arr
<?php
$args = array(
'type' => 'attachment',
'category_name' => 'portfolio'
);
$attachments = get_posts($args);
print_r($attachments);
?>
生成以下结果:
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
)
)
数组
(
[0]=>stdClass对象
(
[ID]=>54
[post_author]=>1
[发布日期]=>2010-06-22 00:32:46
[发布日期格林尼治标准时间]=>2010-06-22 00:32:46
[发布内容]=>
[post_title]=>我们自己的网站
[帖子摘录]=>
[发布状态]=>发布
[评论\u状态]=>打开
[ping_状态]=>打开
[发布密码]=>
[post_name]=>我们自己的网站
[致吴平]=>
[ping]=>
[修改后]=>2010-06-22 00:40:22
[修改后的格林尼治标准时间]=>2010-06-22 00:40:22
[发布内容过滤]=>
[post_parent]=>0
[guid]=>http://localhost/wordpress/?p=54
[菜单顺序]=>0
[职位类型]=>职位
[后置mime类型]=>
[注释计数]=>0
[过滤器]=>原始
)
)
但是,下面的代码不会返回任何内容
<?php
echo wp_get_attachment_image(54, array('300', '300'));
?>
我做错了什么?函数
wp\u get\u attachment\u image
只获取上传到wordpress的图像,它不会在帖子内容中输出图像
您必须输出示例图像的帖子内容
比如:
echo$attachments['post_content']代码>事实上,我认为公认的答案并不能真正回答这个问题
您的问题是您正在将post id(54
在您的示例中;用WP的说法通常是$post->id
)传递到WP\u get\u attachment\u image()。从中可以看出,您应该使用附件id(请参见下面的$attachment\u id
):
换句话说,你必须这样做:
$image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
wp_get_attachment_image函数可以接受四个值,如您所见:
wp_get_attachment_image ( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )
所以我总是用:
<?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) ); ?>
注意:我们可以简单地使用获取\u ID()来传递活动帖子的ID。这里700是附件图像的宽度,600是附件图像的高度。我们也可以通过数组(“class”=>“img responsive”)传递我们的类,所以Wordpress没有一个内部函数来获取特定大小的上传图像?不,你给出的函数是正确的,但是你需要传递附件本身的想法,而不是帖子。啊,好的,谢谢。但是它仍然应该返回大小与指定大小最接近的图像,不是吗?是的,加载附件时它会返回。上面是一个附件,但它不会加载它。这可能是一个错误吗?你必须给它一个附件ID。你做错了的是$args
参数-没有类型
参数,正确的是'post\u type'=>'attachment'
:),因为它默认为post(post\u type
是post
),你给它的是一个“post”ID,而不是“attachment”:
<?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) ); ?>