Support

Account

Home Forums Add-ons Repeater Field Using The Select Field Within The Repeater To Style Different Content Pieces

Helping

Using The Select Field Within The Repeater To Style Different Content Pieces

  • Hello

    I’m familiar with ACF and the Repeater field, but I am trying something new as I finish up my new portfolio. Basically what I’ve done is set up a repeater for all content per project (images, images with a browser border, text only, etc.) so I can edit in one place and reorder them with ease.

    What I’m new to here is the selector field—I just don’t know how to make it work. I’m attaching a screenshot of the content repeater to give you a visual:

    Screenshot of Repeater fields with select fields

    I can get the repeater to work using code like this:

    <?php if( have_rows('project_content') ): ?>
    	<?php while( have_rows('project_content') ): the_row(); 
    		// vars
    		$image = get_sub_field('project_content_image');
    		$browser = get_sub_field('project_content_browser_image');
    		?>
    			<?php if( $image ): ?>
    				<figure>
    					<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    				</figure>
    			<?php endif; ?>
    
    			<?php if( $browser ): ?>
    				<?php if ( 'yes' == get_field('browser_image') ): ?>	
    					<figure class="browser">
    						<div>
    							<span></span><span></span><span></span>
    						</div>
    						<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    						<figcaption><?php echo $image['alt'] ?></figcaption>
    					</figure>
    				<?php endif; ?>
    			<?php endif; ?>
    
    	<?php endwhile; ?>
    <?php endif; ?>

    But I do not know how to integrate the select data into the equation, rather than using lots of if statements (which defeats the whole purpose anyways). The above code is also not savvy to the reordering functionality of the repeater. I’m not super PHP savvy either, so explaining any solution like I’m a child is great. I assume there’s a way in PHP to pass multiple arguments prior to output (ex. this is selected a regular image and it aligns with a variable).

  • Hi @andandandandrew,

    Thanks for the post.

    The get_field() function returns false if (value == “” || value == null || value == false). Thus you will need to make use of the following conditional in your if block:

    <?php if( $browser ): ?>
    				<?php if (get_field('browser_image') ): ?>	
    					<figure class="browser">
    						<div>
    							<span></span><span></span><span></span>
    						</div>
    						<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    						<figcaption><?php echo $image['alt'] ?></figcaption>
    					</figure>
    				<?php endif; ?>
    			<?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Using The Select Field Within The Repeater To Style Different Content Pieces’ is closed to new replies.