Increment variable in WordPress Loop

So, I’m working with a WordPress loop. I’ve created a PHP variable which I’m using to name some id’s and classes. I’d like the variable to be incremented each time so that a new id and class is created. Any idea

It appears that my problem is that it doesn’t know when to increment it. I’m not quite sure

$Number = 1;
$Number++;
$Collapse = 'Collapse'.$Number;

CODE EXAMPLE BELOW

  <!--START of collapsible panels-->	  
                 <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                      <div class="panel panel-default">
                        <div class="panel-heading" role="tab" id="headingOne">
                          <h4 class="panel-title">
                            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                              Collapsible Group Item #1
                            </a>
                          </h4>
                        </div>
                        <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
                          <div class="panel-body">
                            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                          </div>
                        </div>
                      </div>

							<?php while ( have_posts() ) : the_post(); ?>
                
                
                                <?php 
								
								
                
                                // check for rows (parent repeater)
                                if( have_rows('niche_page_side_nav') ): ?>
                                   
                                    <?php 
                
                                    // loop through rows (parent repeater)
                                    while( have_rows('niche_page_side_nav') ): the_row(); 
									
								    $Number = 1;
									$Number++;
									$Collapse = 'Collapse'.$Number;
							
									
									?>
                                     
                                            
											
											
										<div class="panel panel-default">
                                            <div class="panel-heading" role="tab" id="heading<?php echo $Collapse;?>">
                                              <h4 class="panel-title">
                                                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $Collapse;?>" aria-expanded="true" aria-controls="collapse<?php echo $Collapse;?>">
											<?php the_sub_field('links_nav_header'); ?>
                                            	        </a>
                                                  </h4>
                                                </div>
                                            
                                            
                                           <div id="collapse<?php echo $Collapse;?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading<?php echo $Collapse;?>">
											  <div class="panel-body">
                                            <?php 
                							
											
											  
                                            // check for rows (sub repeater)
                                            if( have_rows('nav_links_list') ): ?>
                                                <ul>
                                                <?php 
                
                                                // loop through rows (sub repeater)
                                                while( have_rows('nav_links_list') ): the_row();
                
                                                    // display each item as a list - with a class of completed ( if completed )
                                                    ?>
                                                    <li class="CategoryListLinks"><a class="CategoryListLinksURL" href="<?php the_sub_field('nav_link_url'); ?>"><?php the_sub_field('nav_link_name'); ?></a>
                                                        </br><?php the_sub_field('category_description');?>
                                                    </li>
                                                <?php endwhile; ?>
                                                </ul>
                                                
                                                
                                                
                                                            
                                              </div>
                                            </div>
                                          </div>
                                            <?php endif; //if( get_sub_field('items') ): ?>
                                        
                
                                    <?php endwhile; // while( has_sub_field('to-do_lists') ): ?>
                                 
                                <?php endif; // if( get_field('to-do_lists') ): ?>
                
                            <?php endwhile; // end of the loop. ?>
                            
                            

				</div><!--END of Collapsible Panels-->

I think that I need it to be inside a statement, but I’m not quite sure where?

{
$Number = 1;
$Number++;
$Collapse = ‘Collapse’.$Number;
}

$number = 0 goes outside the loop, to initialize the value.
$number++ goes inside the loop, to increment the value.
$Collapse is unnecessary, but it will work.

1 Like

Thanks that worked perfectly!

I was trying to do a similar thing with some other code, but for some reason the php is being outputted as a string. What’s so different?

indent preformatted text by 4 spaces
    <?php $Number = 1; ?>

                           
                           	<?php while ( have_posts() ) : the_post(); ?>
                           
                           
                           <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                           <?php
						   
						  
									// check if the repeater field has rows of data
									if( have_rows('q&a_repeater') ):
									
										// loop through the rows of data
										while ( have_rows('q&a_repeater') ) : the_row();
										
										
										
							            $Number++;
							           
										
										// display a sub field value
										
										echo '<a href="'.get_field("niche_square_ad_2_url").'"><img src="'.get_field("niche_square_ad_2_image").'" alt=""/></a>';
							 
										echo  '<div class="panel panel-default">';
										echo  '<div class="panel-heading" role="tab" id="heading<?php echo $Number;?>">';
										echo   '<h4 class="panel-title">';
										echo 	'<a class="CollapsibleLink DarkAccentColorBackground" role="button" data-toggle="collapse" data-parent="#accordion" href="Collapse<?php echo $Number;?>" aria-expanded="true" aria-controls="collapseOne">';
											    the_sub_field('question_or_header');
										echo 	'</a>';
										echo 	'</h4>';
										echo    '</div>';	
											
											
											
											the_sub_field('answer_');
									
										endwhile;
									
									else :
									
										// no rows found
									
									endif;
							
							?>
                            <?php endwhile; // end of the loop. ?>

You used single quotation marks

echo  '<div class="panel-heading" role="tab" id="heading<?php echo $Number;?>">';
echo   '<h4 class="panel-title">';
echo 	'<a class="CollapsibleLink DarkAccentColorBackground" role="button" data-toggle="collapse" data-parent="#accordion" href="Collapse<?php echo $Number;?>" aria-expanded="true" aria-controls="collapseOne">';

I should have used double quotes like this?

echo  "<div class="panel-heading" role="tab" id="heading<?php echo $Number;?>">";

Not without escaping the literal quotation marks.

Either way it gets messy (such is WordPress) but maybe concatenating the stings would be easier for you?

http://php.net/manual/en/language.types.string.php

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.