Support

Account

Home Forums ACF PRO Help with Image Array ALT text

Solved

Help with Image Array ALT text

  • Hi,

    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>&pound;</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;
    			?>	

    Thanks

    Cathy

  • Hi Cathy,

    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?

    Thanks πŸ™‚

  • Hi James, I’ve checked it does use local JSON. Changing the value to array in the json file doesn’t do anything, what do I need to do to get the front end to update? Thanks

  • Hi @cathy

    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.

    Thanks πŸ™‚

  • Thanks the sync sorted it πŸ™‚

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

You must be logged in to reply to this topic.