Lucy Brown
Lucy Brown

Reputation: 384

How to display acf image from image array

I have this query -

<?php if (has_term( 'channel', 'listing_area' )) { ?>


<?php
        $posts = get_posts(array(
'numberposts'   => 3,
'post_type'     => 'adverts',
'order_by' => 'title',
'order' => 'random',
'meta_query'    => array(
    'relation'      => 'AND',
    array(
        'key'       => 'associate_adverts',
        'value'     => '1822',
        'compare'   => 'LIKE',
    )
),
 ));

    ?>


  <?php //if it's returning the object

     foreach($posts as $advert){

       $img = get_field("advert", $advert->id);?>

 <img src="<?php echo $img["url"]; ?>"/>

  <?php }?>

I need to show an image called from an advanced custom field called 'advert' which is set to use the image array option where I'm calling the post title.

How can I do this? Thanks.

Upvotes: 2

Views: 6510

Answers (1)

Nozifel
Nozifel

Reputation: 545

Read the get_field doc

In your loop, use $img = get_field("advert", $advert->ID), return an array. Then to get the url, use $img["url"]

The doc

Upvotes: 2

Related Questions