Support

Account

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