0
votes

Im trying to echo an image from a AFC image field, but cant get it to work.

For a regular textfield with a subfield with group name header_usps and subfield name usp_1 im using below code:

<?php $ups1 = get_field('header_usps_usp_1', 'option'); ?>
<?php echo $ups1; ?>

That works great, but when Im trying this with an image field, I cant seems to fetch the url, instead it displays "Array". Image is loaded as URL.

<?php $image1 = get_field('section_one_sideimage_one', 'option'); ?>
<img src="<?php echo $image1; ?>" alt="Image"></p>

Section_one is the group name and sideimage_one the subfield. What is the correct way to do this?

Var_dump:

/app/public/wp-content/themes/bz-onlinehoortoestel/woocommerce/emails/email-footer-quote.php:52:
   array (size=24)
  'ID' => int 156974
  'id' => int 156974
  'title' => string 'sideimage (1)' (length=13)
  'filename' => string 'sideimage-1.png' (length=15)
  'filesize' => int 16778
  'url' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
  'link' => string 'https://localsite/sideimage-1/' (length=44)
  'alt' => string '' (length=0)
  'author' => string '11' (length=2)
  'description' => string '' (length=0)
  'caption' => string '' (length=0)
  'name' => string 'sideimage-1' (length=11)
  'status' => string 'inherit' (length=7)
  'uploaded_to' => int 0
  'date' => string '2020-06-25 09:31:41' (length=19)
  'modified' => string '2020-06-25 09:31:41' (length=19)
  'menu_order' => int 0
  'mime_type' => string 'image/png' (length=9)
  'type' => string 'image' (length=5)
  'subtype' => string 'png' (length=3)
  'icon' => string 'https://localsite/wp-includes/images/media/default.png' (length=68)
  'width' => int 200
  'height' => int 254
  'sizes' => 
    array (size=66)
      'thumbnail' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-140x140.png' (length=82)
      'thumbnail-width' => int 140
      'thumbnail-height' => int 140
      'medium' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'medium-width' => int 200
      'medium-height' => int 254
      'medium_large' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'medium_large-width' => int 200
      'medium_large-height' => int 254
      'large' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'large-width' => int 200
      'large-height' => int 254
      '1536x1536' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      '1536x1536-width' => int 200
      '1536x1536-height' => int 254
      '2048x2048' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      '2048x2048-width' => int 200
      '2048x2048-height' => int 254
      'small' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'small-width' => int 200
      'small-height' => int 254
      'logo' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'logo-width' => int 200
      'logo-height' => int 254
      'posts-archive' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'posts-archive-width' => int 200
      'posts-archive-height' => int 254
      'watermark' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-200x250.png' (length=82)
      'watermark-width' => int 200
      'watermark-height' => int 250
      'logo-small' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-33x42.png' (length=80)
      'logo-small-width' => int 33
      'logo-small-height' => int 42
      'header-product' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-170x216.png' (length=82)
      'header-product-width' => int 170
      'header-product-height' => int 216
      'best-sold' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-114x145.png' (length=82)
      'best-sold-width' => int 114
      'best-sold-height' => int 145
      'mini-cart' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-90x90.png' (length=80)
      'mini-cart-width' => int 90
      'mini-cart-height' => int 90
      'product-selected' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-200x219.png' (length=82)
      'product-selected-width' => int 200
      'product-selected-height' => int 219
      'favicon' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-64x64.png' (length=80)
      'favicon-width' => int 64
      'favicon-height' => int 64
      'woocommerce_thumbnail' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'woocommerce_thumbnail-width' => int 200
      'woocommerce_thumbnail-height' => int 254
      'woocommerce_single' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'woocommerce_single-width' => int 200
      'woocommerce_single-height' => int 254
      'woocommerce_gallery_thumbnail' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-100x100.png' (length=82)
      'woocommerce_gallery_thumbnail-width' => int 100
      'woocommerce_gallery_thumbnail-height' => int 100
      'shop_catalog' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'shop_catalog-width' => int 200
      'shop_catalog-height' => int 254
      'shop_single' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1.png' (length=74)
      'shop_single-width' => int 200
      'shop_single-height' => int 254
      'shop_thumbnail' => string 'https://localsite/wp-content/uploads/2020/06/sideimage-1-100x100.png' (length=82)
      'shop_thumbnail-width' => int 100
      'shop_thumbnail-height' => int 100
1
echo $image1['url']; for fullsize - echo $image1['sizes']['thumbnail']; for thumbnail size etc.. echo $image1['alt']; for the alt text - Stender
else you can go into your registration of the image field, and set the field to return the URL instead of the array - Stender

1 Answers

1
votes

To access the subfield you first need to get the group like so :

$section_one = get_field("section_one");

You can then get the subfield through this variable :

$image1 = $section_one["sideimage_one"];

If you want to get the url of the image, make sure when creating the field that the return format is set to "URL".

Hope this helps.