【问题标题】:Wordpress Advanced Custom Field - display image in specified sizeWordpress 高级自定义字段 - 以指定大小显示图像
【发布时间】:2015-09-10 01:46:46
【问题描述】:

我按照 ACL 网站的说明以特定尺寸显示图像,但它根本不显示。我在转发器字段中将图像设置为对象,这是我的代码:

$image_1 = get_sub_field('image');

<?php if($image_1): ?>
 <img src="<?php echo $image_1['sizes']['medium']; ?>" alt="" />
<?php endif; ?>

当我删除以下内容时,它显示图像正常,但当然是全尺寸:

['sizes']['medium']

有什么问题?是否应该在某处添加 ['url'] ? 我总是对 ACL 中的那些图像感到困惑。

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    您必须指明您的图像索引,因为通常您会在循环中运行此代码,因此 image_1 将是一个数组,要访问您需要添加图像位置的第一张图像,image_1[0]['sizes'] ['medium'] .. 我试过这段代码,它工作正常。

    if ( have_rows( 'repeater_field', $homepage_id ) ) {
    
        $i = 0;
        // loop through the rows of data
        while ( have_rows( 'repeater_field', $homepage_id ) ) {
            the_row();
            //Get imgs
            $imgs[$i]['img_test'] = get_sub_field( 'img_test' );
            $i++;
        }
    }
    
    
    echo "src = ".$imgs[0]['img_test']["sizes"]["medium"];
    

    【讨论】:

    • 我采用了不同的方法,整理了我的代码,但你的回答也很好。
    【解决方案2】:

    ACF 可以选择图片的默认尺寸和自定义尺寸。

    这样用,

    $images = get_sub_field('image'); 
    
     wp_get_attachment_image_src($images, 'full');
    
     wp_get_attachment_image_src($images, 'medium');
    
     wp_get_attachment_image_src($images, 'thumbnail');
    

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 2018-04-15
      • 2017-01-06
      • 2013-02-04
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2021-06-12
      相关资源
      最近更新 更多