Home › Forums › Add-ons › Repeater Field › 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:
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; ?>
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.