[英]Advanced Custom Fields - thumbnails
Using Advanced Custom Fields (ACF), I usually use the following code to display a specific field with a specific thumbnail size (using image ID): 使用高级自定义字段 (ACF),我通常使用以下代码来显示具有特定缩略图大小的特定字段(使用图像ID):
$image = get_field('field_name_here');
$size = 'thumbnail_size_here';
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
However, when I tried to use this code in a repeater field, it just spits out numbers. 但是,当我尝试在转发器字段中使用此代码时,它只会吐出数字。
I have tried updating the above code to: 我尝试将上述代码更新为:
$image = the_sub_field('field_name_here');
$size = 'thumbnail_size_here';
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
However, this does not work, again it just spits out numbers. 但是,这不起作用,它只会吐出数字。 The full code would be like this:
完整的代码如下:
<?php if(get_field('example_repeater_field')): ?>
<ul>
<?php while(has_sub_field('example_repeater_field')): ?>
<li>
<?php
$image = the_sub_field('field_name_here');
$size = 'thumbnail_size_here';
if( $image ) {
echo wp_get_attachment_image( $image, $size );
} ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
I have read the info on the ACF website but cannot see anything that related to custom thumbnail sizes. 我已经阅读了ACF网站上的信息,但看不到与自定义缩略图大小相关的任何内容。
Has anyone come across this before and found a solution? 有人遇到过这个问题并找到了解决方案吗?
I'm not a huge fan of while
loops and multiples requests, so I usually prefer to get my data with only one request then loop through it with a foreach
, like this : 我不是
while
循环和多重请求的忠实拥护者,因此我通常更喜欢只使用一个请求获取数据,然后使用foreach
它,如下所示:
<?php $fields = get_field('repeater_field'); ?>
<?php foreach ($fields as $field): ?>
<?= $field['field_name']; ?>
<?php endforeach; ?>
Also, you could try to have the image field set to return the image object (instead of the id), and to display the image like this : (see the "Customized display (Object)" section) 另外,您可以尝试将image字段设置为返回图像对象(而不是id),并像这样显示图像:( 请参阅“自定义显示(对象)”部分)
<?php $image = get_field('image'); ?>
<?php echo $image['sizes']['thumbnailSize']; ?>
Which in your case would give us something like this (once you've set your acf field to return an object) : 在您的情况下,这会给我们这样的信息(一旦您将acf字段设置为返回对象):
<?php $fields = get_field('repeater_field'); ?>
<?php if( !empty($fields) ): ?>
<ul>
<?php foreach ($fields as $field): ?>
<li><?= $field['field_name']['sizes']['thumbnail_size']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I've got exactly this in a repeater field loop 我在转发器字段循环中有这个
<?php if(have_rows('column')):
while(have_rows('column')) : the_row(); ?>
<?php $image = get_sub_field('image');
if( !empty($image) ):
$url = $image['url'];
$caption = $image['caption'];
// thumbnail size (column) declared in functions.php
$size = 'column';
$thumb = $image['sizes'][ $size ]; ?>
<?php endif; ?>
<img src="<?php echo $thumb; ?>">
But this only works if your return value is set as "image array" in ACF 但这仅在您将返回值在ACF中设置为“图像数组”的情况下有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.