Using Advanced Custom Fields in a widget

I’ve built a widget to show pages from a certain category in a dropdown menu. The page that’s selected will show up on the homepage. This all works, except the Advanced Custom Fields (ACF) are all showing the same image. I think it’s to do with $instance…

The code I’m using is:

    <div class="home-image-box">

    <?php $attachment_id = get_field('main_image');
    $size = "home-boxes";
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    ?>

    <a href="https://wordpress.stackexchange.com/questions/98427/<?php echo get_permalink($instance["post_id']); ?>">
    <img class="headline" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" src="<?php echo $image[0]; ?>" />
    </a>
    </div>

    <p class="extra-text"><?php the_field('sub_title'); ?></p>

All the code works great on the individual pages – it’s just when called by the widget that I get these issues. The title and permalink are using the normal WordPress functions, and these work fine too.

Are there any ACF users here that can shed some light on this?

Thanks

1 Answer
1

Try passing the post ID as the second parameter:

the_field('sub_title', $instance['post_id']);

Leave a Comment