Home › Forums › ACF PRO › Echo closing tag after the last row, if rows are not empty › Reply To: Echo closing tag after the last row, if rows are not empty
Hey James and thanks for the reply,
It seems like there is a problem with $step_num=count($rows);
, it returns 1
and therefore the if($i == $step_num)
statement occurs on the first iteration and not the last.
Is there a better way to count the repeater rows?
EDIT: Solution!
It’s impossible to count have_rows()
, the right way is counting the get_field()
itself. so easy ๐
function itamar_index_for_instructubles() {
// check if the instructuble has steps
if( have_rows('ืฉืืืื') ):
$i=0;
$rows = get_field('ืฉืืืื');
$step_num=count($rows);
// loop through the steps
while ( have_rows('ืฉืืืื') ) : the_row();
$i++;
$step_title = get_sub_field('ืฉื_ืืฉืื');
if($step_title!='') :
// first iteration
if($i == 1) :
//open tags for the table of content
$indexed = '<div class="info-table" id="index"><div class="title">ืชืืื ืขื ืืื ืื<span id="oc-index">-</span></div><div class="body"><ul class="indexed-content">';
endif;
$indexed .= '<li>'.$i.'. <a href="#anc'.$i.'">'.$step_title.'</a></li>' ;
// last iteration
if($i == $step_num-1) :
//closing tags for the table of content
$indexed .= '</ul></div><div class="bottom"></div></div>';
endif;
endif;
endwhile;
echo $indexed;
endif;
}
Thanks!
Itamar
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!
๐ฃ โACF Chat Fridaysโ
— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
Weโd love to see you there!
๐ Friday 3rd Feb - 3pm UTC
๐ Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 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.