How to use size class in Advanced Custom Fields WP?

时间:2016-06-18 20:20:19

标签: php wordpress advanced-custom-fields

I am using Advanced custom fields in my WordPress theme to manage my slider images of Flexslider. I upload the images high quality in the backend, but I only want to show the images with a size of 960px width.

In the documentation is mentioned that the size is stored in the [sizes] array.

https://www.advancedcustomfields.com/resources/gallery/

Does anyone know if the custom sizes that are created in the functions.php file will exist also in the stored array?

And my second question is to get access of a specific size class, should you still use [url] to get the image url or is it automatically stored in the array of sizes?

So is this the correct markup:

<?php foreach( $images as $image ): ?>
    <li>
        <img src="<?php echo $image['url']['sizes']['slider-image']; ?>" alt="<?php echo $image['alt']; ?>">
    </li>
<?php endforeach; ?>

or:

<?php foreach( $images as $image ): ?>
    <li>
        <img src="<?php echo $image['slider-image']; ?>" alt="<?php echo $image['alt']; ?>">
    </li>
<?php endforeach; ?>

1 个答案:

答案 0 :(得分:1)

是的,您定义的自定义尺寸将在['sizes']数组中返回。

每个的URL单独返回,作为大小的名称。例如,如果您的自定义尺寸称为“滑块图片”,则会在['sizes']['slider-image']找到图片的网址。

如果您需要,还可以在['sizes']['slider-image-width']['sizes']['slider-image-height']访问图片的尺寸(当图片不符合您所期望的确切纵横比时,这可能很有用,意味着调整大小的图像的较小轴不会与您的自定义尺寸尺寸完全匹配。)

您可以在以后找到PHP函数print_r(),以便发现可用的数据。