缩略图图像大小的输出URL

时间:2017-08-28 16:49:42

标签: php advanced-custom-fields

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

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

但是,我希望能够保留自定义大小,但只输出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 
    ) 
)

1 个答案:

答案 0 :(得分:1)

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

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

e.g。

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

修改

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

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