Home › Forums › Add-ons › Repeater Field › First row skipped when two on same page.
I'm using a repeater field on the following page - http://emqff.org/emqffnewsite/about/success-stories (this page is working great). The name of the repeater is "successbox" and it has subfields called "photo", "title" subtitle" and "link".
I'm trying to pull these fields into the homepage - http://emqff.org/emqffnewsite/
In the blue box I wanted to pull the content from the first two stories, so I used the following code...
<?php
if(get_field(successbox, ‘117’)):
$i=0;
while(has_sub_field(successbox, ‘117’)):
echo ‘
‘;
if($i<1) $i++;
else break;
endwhile;
endif;
?>
`
This worked great, and pulled content from the first two stories (“Billy” and “Marciel”).
I then added the following code snippet to pull the photo from the first story…
<img style="width:89px; height: 107px;" src="
<?php
$i=0;
while (the_repeater_field(successbox, '117')):
the_sub_field(photo, '117');
if($i<0) $i++;
else break;
endwhile;
?>
" alt="" border="0"></div>
This also worked fine. But a strange thing happened. After adding this second code snippet, the original code snippet no longer starts with “Billy” (item #1), it now starts with “Maricel” (item #2).
The two snippets must be aware of each other somehow, but I can’t seem to find a way to make both snippets work correctly. They both work correctly when they’re the only snippet on the page… but when I put both snippets on the page, the list on the left starts with the second story (it should start with “Billy” – the first story).
Can someone please suggest a workaround?
(I would really appreciate some help)
Hi @idesign123
This reason for this is because the repeater field is not being ‘cleared’ because you are using ‘break’ to end the loop early. Then when you loop through the repeater field again, it continues from where it is up to. Does this make sense?
To fix the issue, you will need to either not use has_sub_field and instead, use a simple foreach loop (please see repeater field docs for this)
or, you can not use break, but wrap your sub field code within an if statement to only work when $i<0
Hope that helps.
Thanks
E
I totally understand your explanation – thanks!
Ok, now the problem is that I’m a hack at PHP and don’t know how to write it very well 😛
Here are two attempts I made that didn’t work…
ATTEMPT #1 – WRAP IN FOREACH
Error Message = “Warning: Invalid argument supplied for foreach()”
<ul>
<?php
if(get_field(successbox, '117')):
foreach($rows as $row) {
$i=0;
echo '<li><a href="';
the_sub_field(link, '117');
echo '">';
the_sub_field(title, '117');
echo '</a><br>';
the_sub_field(subtitle, '117');
echo '</li>';
if($i<1) $i++;
else break;
}
endif;
?>
</ul>
ATTEMPT #2 – SEE REPEATER FIELD DOCS
Error Message = Parse error: syntax error, unexpected ‘,’, expecting ‘]’
<ul>
<?php
$rows = get_field(successbox, '117');
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row[title, '117'] . ', sub_field_2 = ' . $row[subtitle, '117'] .', etc</li>';
}
echo '</ul>';
}
?>
</ul>
Can you please help me with my code snippet?
I’m really trying to learn PHP, but am struggling.
What I’m trying to produce is…
SCRIPT 1…
<subfield ‘title’ from row 1>
<subfield ‘subtitle’ from row 1>
<subfield ‘title’ from row 2>
<subfield ‘subtitle’ from row 2>
SCRIPT 2…
<subfield ‘photo’ from row 1>
Thank you for helping a PHP newbie!
Hi @idesign123
Your issue with the above code is this: $row[title, '117']
this is not how a PHP array works.
Please read over the docs and also read up on PHP. This is a developer plugin and I must expect you to understand how to use an array otherwise this would just be a free dev service, not a support forum.
Thanks
E
The topic ‘First row skipped when two on same page.’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.