Support

Account

Home Forums Add-ons Repeater Field Gallery field don't work in frontend as a sub_field of a Repeater

Solved

Gallery field don't work in frontend as a sub_field of a Repeater

  • I can’t show in frontend the gallery field I’ve created on a sub_field of a Repeater.

    I use this code:

    
    <?php
    				// Create the Markup for the Custom Gallery
    				$images = the_sub_field('galleria_pianta');
    				if($images): ?>
    				
    				<div id="ok_gallery">
    				<?php foreach( $images as $image ): ?>
    					<a class="lightbox" data-fancybox-group="gallery-<?php the_ID(); ?>" title="<?php echo $image['title']; ?> <?php echo $image['description']; ?>" href="<?php echo $image['url']; ?>">
    						<img src="<?php echo get_template_directory_uri(); ?>/img/load.gif" data-src="<?php echo $image['sizes']['thumbnail']; ?>" data-src-retina="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
    						<?php echo $image['title']; ?>
    					</a>
    				<?php endforeach; ?>
    				<div class="clearfix"></div>
    				</div>
    				
    				<?php endif; ?>
    

    I get two error ( “2” like the number of the image in the gallery ) like this:

    Notice: Array to string conversion in /Applications/MAMP/htdocs/nm/wp-content/plugins/advanced-custom-fields/core/api.php on line 610

  • Hi @boniz

    I believe the issue is due to misusing the the_sub_field function.

    Your first line of code should be changed to:

    
    $images = get_sub_field('galleria_pianta');
    

    So, to clarify, please use get_sub_field to get data.

    Thanks
    E

  • Getting the exact same error but from a much simpler sub field usage, I’m sure its just something I’m not writing out correctly:

    
    <?php if( get_field('slide') ): ?>
    	<?php while( has_sub_field('slide') ): ?>
    		<?php the_sub_field('slide_image'); ?>
    		<?php the_sub_field('slide_title'); ?>
    		<?php the_sub_field('slide_subtitle'); ?>
    	<?php endwhile; ?>
    <?php endif; ?>
    
  • Hi @elliot

    The error in question (array to string conversion) is self explanatory. One of the values for your sub fields is actually an array. Most likely the image sub field.

    You can echo an array in PHP, that just doesn’t work.

    So instead, you need to read the docs and use get_sub_field instead to correctly render out the HTML you want.

    Thanks
    E

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

The topic ‘Gallery field don't work in frontend as a sub_field of a Repeater’ is closed to new replies.