在wordpress中使用wp_get_attachment_image()的正确方法

发布于 2024-09-07 04:29:34 字数 1741 浏览 3 评论 0原文

我正在寻找使用 wp_get_attachment_image() 的正确方法。

以下代码:

<?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
        )
)

但是,以下代码不会返回任何内容。

<?php
    echo wp_get_attachment_image(54, array('300', '300'));
?>

我在这里做错了什么?

I'm looking for the correct way of using 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箜明 2024-09-14 04:29:34

事实上,我认为接受的答案并没有真正回答问题。

您的问题是您传入了帖子ID(示例中的54;在WP用语中通常为$post->ID)到wp_get_attachment_image()。正如 codex 中所示,您应该使用 附件 id(请参阅下面的 $attachment_id):

wp_get_attachment_image( $attachment_id, $size, $icon );

换句话说,您必须执行以下操作:

$image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');

Actually, I don't think the accepted answer really answers the question.

Your problem is that you're passing in the post id (54 in your example; typically $post->ID in WP parlance) to wp_get_attachment_image(). As can be seen in the codex, you're supposed to use the attachment id (see $attachment_id below):

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');
物价感观 2024-09-14 04:29:34

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" ) );  ?>

注意:我们可以简单地使用 get_the_ID() 来传递活动帖子的 id。这里700是宽度600是附件图像的高度。我们还可以将我们的类作为 array( "class" => "img-responsive" ) 传递

wp_get_attachment_image function can accept four values as you can see:

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 width and 600 is height of attachment image. we can also pass our class as array( "class" => "img-responsive" )

小巷里的女流氓 2024-09-14 04:29:34

函数wp_get_attachment_image仅获取上传到wordpress的图像,它不会输出帖子内容中的图像。

您必须输出示例图像的帖子内容。

例如:echo $attachments['post_content'];

The function wp_get_attachment_image only gets an image that was uploaded to wordpress, it doesn't output an image in the content of the post.

You have to output the content of the post for your example image.

Like: echo $attachments['post_content'];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文