缩略图图像大小的输出URL



我在ACF中使用以下代码显示图像,使用特定的缩略图使用图像ID。

<?php 
    $image = get_field('services-hero-bg'); $size = 'hero';
    if( $image ) {
        echo wp_get_attachment_image( $image, $size );
    }
 ?>

但是,我希望能够保持自定义尺寸,但仅输出URL。

我已经浏览了文档并找到了输出URL的方法,但这不允许您选择自定义图像大小。

更新

print_r的结果:

Test Array ( 
    [ID] => 158 
    [id] => 158 
    [title] => Marquees-4 
    [filename] => Marquees-4.jpg 
    [url] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4.jpg 
    [alt] => 
    [author] => 1 
    [description] => 
    [caption] => 
    [name] => marquees-4 
    [date] => 2017-08-28 16:17:52 
    [modified] => 2017-08-28 16:17:56 
    [mime_type] => image/jpeg 
    [type] => image 
    [icon] => http://localhost:8888/wp-includes/images/media/default.png 
    [width] => 1024 
    [height] => 683 
    [sizes] => Array ( 
        [thumbnail] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-150x150.jpg 
        [thumbnail-width] => 150 [thumbnail-height] => 150 
        [medium] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-250x167.jpg 
        [medium-width] => 250 
        [medium-height] => 167 
        [medium_large] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-768x512.jpg 
        [medium_large-width] => 768 
        [medium_large-height] => 512 
        [large] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-700x467.jpg 
        [large-width] => 700 
        [large-height] => 467 
        [small] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-120x80.jpg 
        [small-width] => 120 [small-height] => 80 
        [logo] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-400x267.jpg 
        [logo-width] => 400 [logo-height] => 267 
        [hero] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-600x683.jpg 
        [hero-width] => 600 
        [hero-height] => 683 
    ) 
)

如果字段类型为 image,则 get_field应该返回数组。在数组中,有一个称为url的参数。

<?php 
    $image = get_field('services-hero-bg'); $size = 'hero';
    if( $image ) {
        echo $image['url'];
    }
?>

,例如

<?php 
    $image = get_field('services-hero-bg'); $size = 'hero';
    if( $image ) {
        echo '<img src="'.$image['url'].'">';
    }
?>

编辑

添加了print_r($image)的输出后,我可以看到您只需要使用它来访问英雄图像的URL:

echo $image['sizes']['hero'];

最新更新