Support

Account

Home Forums Add-ons Flexible Content Field Count flexible field sub fields

Solved

Count flexible field sub fields

  • I’m just trying to get a count of this “page_break” layout. There are two being used, and the code returns “1 1”.

    I know this is a problem with my counter, but I’ve tried everything and I’ve only managed to create endless loops.

    <?php 
      
     // $count = 0;
      
      while( has_sub_field("content") ) : ?> 
    
        <?php if(get_row_layout() == "page_break") : ?>
    		
          <?php echo $count; ?>
          
          <?php endif; ?>
          
          <?php //$count++; ?>
          
    <?php endwhile; ?>

    You can see how I have my fields set up here:
    http://f.cl.ly/items/273l1L2w3X2r3E2L1b2d/Screen%20Shot%202013-12-12%20at%2011.50.05%20PM.png

    Actually, if I could count the “break_here” sub field, that would be even better.

  • Hi @freshyill

    Something like the below should work for you:

    <?php 
    $count = 0;
    while( has_sub_field("content") ) : 
    
        if(get_row_layout() == "page_break") :
    	
            if(get_sub_field('break_here')){
               $count++;	
               echo $count;
            }
          
        endif;
          
    endwhile; ?>
    
  • Hi @FourStacks, this isn’t quite it. This echoes and then increments each one.

    So if there are five, the result is this:

    12345

    but what I’m looking for just 5.

    Does that make sense?

  • Looks like I was just getting the page count in the wrong place. Here’s a working solution.

    <?php $pagecount = 0;
    
    while( has_sub_field("content") ) : 
      
      if(get_row_layout() == "page_break") :
      
        if(get_sub_field('break_here')){
          $pagecount++;	
        }
        
      endif;
    endwhile;
    ?>

    Then I can just use $pagecount elsewhere:

    <?php echo $pagecount; ?>

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Count flexible field sub fields’ is closed to new replies.