Support

Account

Home Forums Front-end Issues pass page specific ACF image array values outside of the loop

Solved

pass page specific ACF image array values outside of the loop

  • I have some ACF(advanced Custom Fields) image values that I display in an include file outside of the main loop. The ACF image is stored as an array, so I would like to display the array values, for example URL, alt tag, size , ect..

    This is my code

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $callout_image = get_post_meta($postid, 'callout_image', true);
    wp_reset_query();
    ?>
    
            <?php if( !empty( $callout_image ) ): ?>
                <img src="<?php echo esc_url($callout_image['url']); ?>" alt="<?php echo esc_attr($callout_image['alt']); ?>" />
            <?php endif; ?>

    the error is Warning: Illegal string offset ‘url’ in

    What am I doing wrong?

  • This is because you are using get_post_meta(). The value stored in the DB for an image is just the post ID of the image. Using ACF get_field() is required to get the array.

  • Hello John,

    thank you for responding

    I did try that and got no results, I just want to point out that this code is running outside the loop. Is there something I need to adjust with that?

  • 
    $callout_image = get_field('callout_image', $postid);
    

    should do it.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.