Home › Forums › Add-ons › Repeater Field › Looking for a more neat solution to returning specific rows from a repeater
Hi,
I have a repeater field from which I need to return values from specific rows to different locations in my markup. I got my code to work, but I don’t believe it’s a neat solution, and was looking for something better.
This is was I currently have:
<?php
$stripTagExcept = '<br> <strong> <em> <u> <del> <a> <img> <ul> <ol> <li> <span> <blockquote>';
if( have_rows('home_steps_steps', 2) ) {
while( have_rows('home_steps_steps', 2) ) { the_row();
if( get_sub_field('home_steps_steps_id') == 1 ) {
$stepIconOne = get_sub_field('home_steps_steps_icon');
$stepTitleOne = get_sub_field('home_steps_steps_title');
$stepParagraphOne = get_sub_field('home_steps_steps_paragraph');
$stepPOneFormat = strip_tags($stepParagraphOne, $stripTagExcept);
}
if( get_sub_field('home_steps_steps_id') == 2 ) {
$stepIconTwo = get_sub_field('home_steps_steps_icon');
$stepTitleTwo = get_sub_field('home_steps_steps_title');
$stepParagraphTwo = get_sub_field('home_steps_steps_paragraph');
$stepPTwoFormat = strip_tags($stepParagraphTwo, $stripTagExcept);
}
if( get_sub_field('home_steps_steps_id') == 3 ) {
$stepIconThree = get_sub_field('home_steps_steps_icon');
$stepTitleThree = get_sub_field('home_steps_steps_title');
$stepParagraphThree = get_sub_field('home_steps_steps_paragraph');
$stepPThreeFormat = strip_tags($stepParagraphThree, $stripTagExcept);
}
if( get_sub_field('home_steps_steps_id') == 4 ) {
$stepIconFour = get_sub_field('home_steps_steps_icon');
$stepTitleFour = get_sub_field('home_steps_steps_title');
$stepParagraphFour = get_sub_field('home_steps_steps_paragraph');
$stepPFourFormat = strip_tags($stepParagraphFour, $stripTagExcept);
}
if( get_sub_field('home_steps_steps_id') == 5 ) {
$stepIconFive = get_sub_field('home_steps_steps_icon');
$stepTitleFive = get_sub_field('home_steps_steps_title');
$stepParagraphFive = get_sub_field('home_steps_steps_paragraph');
$stepPFiveFormat = strip_tags($stepParagraphFive, $stripTagExcept);
}
}
}
?>
I basically added an addition field in which I added a unique ID (1-5 for now) and use this field to return the values in a variable.
Is there a neater way to do this? I have only been working with PHP for the last 2 months, so I wouldn’t know of anything better at the moment. I also looked up the ACF documentation, but it didn’t get me really far for once. Thanks in advance!
I would use a switch statement, they are cleaner when you have many possiblities
$steps = get_sub_field('home_steps_steps_id');
switch ($steps) {
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
case 4:
// do something
break;
case 5:
// do something
break;
}
https://www.php.net/manual/en/control-structures.switch.php
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!
Our guide looks into WordPress custom fields and how they stack up against the possibilities of ACF.
— Advanced Custom Fields (@wp_acf) July 18, 2022
https://t.co/hk3yibkHyk
© 2022 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.