Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 5 years ago.
Improve this questionI love ACF, its great for developers and it seems to have so much support. BUT... Why is reset_rows() not in the documentation? I created a function that can delete repeater rows at different times in the code. I was using the rows loop each time expecting it to start at index 0 (or 1 in acf's crazy way). But each loop started where the previous one left off.
This gave me grief for quite a while. I have only found ONE other topic with a small 3 word reply mentioning reset_rows(). ACF documentation has nothing that comes up when you searched reset_rows() or anything about reseting the row index. ITS NO WHERE!
So maybe this can save others from the grief. Here is how i use it successfully:
reset_rows('field_name', $post_id);
$post_id could be optional, havnt tried it..
Here is how i have placed it in my code
if( have_rows('related_trustees', $memberID) )
{
reset_rows('related_trustees', $memberID); // Start from begining (protects against previous loops)
echo 'Have rows';
$rowNum = 0; // ACF Rows start at index of 1
while( have_rows('related_trustees', $memberID) )
{
the_row();
$rowNum ++;
if(get_sub_field('trustee') == $userID)
{
$delrow = delete_row('related_trustees', $rowNum, $memberID);
break;
}
}
}
Let me know if theres a reason to this madness please.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 5 years ago.
Improve this questionI love ACF, its great for developers and it seems to have so much support. BUT... Why is reset_rows() not in the documentation? I created a function that can delete repeater rows at different times in the code. I was using the rows loop each time expecting it to start at index 0 (or 1 in acf's crazy way). But each loop started where the previous one left off.
This gave me grief for quite a while. I have only found ONE other topic with a small 3 word reply mentioning reset_rows(). ACF documentation has nothing that comes up when you searched reset_rows() or anything about reseting the row index. ITS NO WHERE!
So maybe this can save others from the grief. Here is how i use it successfully:
reset_rows('field_name', $post_id);
$post_id could be optional, havnt tried it..
Here is how i have placed it in my code
if( have_rows('related_trustees', $memberID) )
{
reset_rows('related_trustees', $memberID); // Start from begining (protects against previous loops)
echo 'Have rows';
$rowNum = 0; // ACF Rows start at index of 1
while( have_rows('related_trustees', $memberID) )
{
the_row();
$rowNum ++;
if(get_sub_field('trustee') == $userID)
{
$delrow = delete_row('related_trustees', $rowNum, $memberID);
break;
}
}
}
Let me know if theres a reason to this madness please.
Share Improve this question asked Aug 23, 2019 at 2:46 jonnyKjonnyK 912 silver badges5 bronze badges 6- 3 I understand ACF is a third party plugin. But ACF's popularity in Wordpress should give it some leeway. – jonnyK Commented Aug 26, 2019 at 17:44
- Yeah, I totally agree. I've been using ACF for years, and I don't do a WP project without it. Your tip fixed a problem for me that seems to have been introduced in the most recent version of ACF, so thanks! – Nate Commented Jul 13, 2020 at 21:13
- I take that back -- not a bug in ACF. Turns out I was having the problem because of another area where I was going through the same loop. But reset_rows() allowed me to fix the issue, so thanks again! – Nate Commented Jul 13, 2020 at 21:23
- Np! The good old power of stack exchange, let one of us trip so the rest can walk around the stump. – jonnyK Commented Jul 14, 2020 at 23:30
- 2 there should be a wordpress-plugins.stackexchange... so this would be ON-topic... sigh. – JJS Commented Jan 20, 2021 at 18:25
1 Answer
Reset to default 8It's not in the documentation mainly because it's an internal function, they weren't expecting you to use or need it.
have_rows()
checks to see whether there is a currently active loop- if not, it creates a global with your repeater rows; if so, it picks up where you left off- then it lets you know whether there are rows remaining to be looped through. Your example is intriguing, seems to be a bit of a fringe scenario, but I can see how frustrating it would be. After jumping out of the loop midstream, you aren't automatically returned to the beginning because the previous loop is still active and has rows remaining.
ACF doesn't have a true rewind_posts()
equivalent, but you can either run the reset_rows()
function or the function it's wrapping: acf_remove_loop()
(also internal and undocumented, sorry!). Whichever you use should probably be run right before your break
though, so you don't end up with unexpected results from your if ( have_rows() )
should you ever be removing the last row and following it up with another loop.
An unrelated but interesting tidbit is that you actually don't need the custom row counter, they provide get_row_index()
for just this purpose.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745222364a4617307.html
评论列表(0条)