Wordpress:获取附加图像的高度和宽度

col*_*unn 4 php wordpress

在WordPress中,我从帖子中抓取附加图像并将其显示在无序列表中.它的伟大工程,但我现在需要获得图像heightwidth,在我得到了同样的方式src.这是我的代码:

PHP

$post_thumbnail_id = get_post_thumbnail_id( $iPostID );
foreach( $arrKeys as $key) {
    if( $key == $post_thumbnail_id )
        continue;
    $sImageUrl = wp_get_attachment_url($key);
    $sImgString = '<li><img src="' . $sImageUrl . '" alt="Thumbnail Image" /></li>';
    echo $sImgString;
}
Run Code Online (Sandbox Code Playgroud)

知道我怎么能这样做吗?

JKi*_*rtz 9

您可以使用wp_get_attachment_image_src(),它应该返回一个数组('/path/to/img.jpg',32,64),其中32是宽度,64是高度...阅读wordpress codex以获取有关此函数用法的更多信息.

  • 您应该添加 'full' 作为第二个参数。 (2认同)

Waq*_*gir 5

你也可以这样做:

$image_attributes = wp_get_attachment_image_src( $attachment_id = 8 );
if ( $image_attributes ) : ?>
    <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)