Wondering if anyone has any ideas. I’ve been asked to edit some existing code on a WordPress website which uses your pro plugin, the requirement is to pull out the ALT text for an image in the template. The template uses a repeater field to display different hosting options, each option has a background image which is a sub field and this image’s alt text required to be shown in the image tag.
The code I have added are the two $bgimage parts
I’ve set the background image field to Image Array, however when I dump the data from the field I just get a string URL (URL was what the field value was set to originally). My image is still showing correctly on the front end using the existing code which says to me it is still using the URL value rather than saving as array.
The alt tag is then showing as Warning: Illegal string offset ‘alt’ in….
My code is below:
<?php // check if the repeater field has rows of data if( have_rows('hosting_package') ): // loop through the rows of data while ( have_rows('hosting_package') ) : the_row(); $hosting_popular = get_sub_field('hosting_popular'); $bgimage = get_sub_field('hosting_background'); ?> <li> <h3><?php the_sub_field('hosting_name'); ?></h3> <h4><span class="from">FROM</span><br/><strong><span>£</span><?php the_sub_field('hosting_price'); ?></strong> /pm<br /> <em>+VAT @ 20%</em></h4> <p class="button"><a href="<?php the_sub_field('hosting_link'); ?>">SELECT</a></p> <p class="desc"><?php the_sub_field('hosting_features'); ?></p> <div class="benefits"><?php the_sub_field('hosting_benefits'); ?></div> <img class="bg" src="<?php the_sub_field('hosting_background'); ?>" alt="<?php echo $bgimage['alt']; ?>" /> </li> <?php endwhile; else : // no rows found endif; ?>
That’s weird. If you changed the “Return Value” to “Image Array”, then the dumped data shouldn’t show the image URL. Could you please make sure that the theme doesn’t use the Local JSON or registers the fields via PHP code?
Also, could you please share the JSON export file of your field group so I can test it out?
Could you please try to sync the Local JSON files? This page should give you more idea about it: https://www.advancedcustomfields.com/resources/synchronized-json/.
If that doesn’t work, could you please backup the Local JSON folder and then delete it from your site? After that, please check the “Return Value” on the backend to make sure that it has “Image Array” enabled.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.