Support

Account

Home Forums Front-end Issues Count total number of post objects

Solved

Count total number of post objects

  • I’m trying to do a count of the total number of post objects selected so that if it’s 1,2 or 3 it shows them in a certain method;

    e.g. 1 = full width, 2 = half width etc.

    My code is;

    <?php 
    	if( have_rows('featured_developments') ): 
    	$count = 0;
    ?>          
        <?php while( have_rows('featured_developments') ): the_row(); ?>    
            <?php
                $post_id = get_sub_field('development', false, false);
                $rows = get_sub_field('development');
                if( $post_id ): 	
            	$count++;
            ?>						
    			<?php if ($count == 1) { ?>
    				1
    			<?php } else if ($count == 2 ) { ?>
    				2
    			<?php } else { ?>
    				3
    			<?php } ?> 
            <?php endif; ?>      
        <?php endwhile; ?>
    <?php endif; ?> 
    <?php wp_reset_query(); ?>

    However the just outputs 1 2 for some reason, despite only 2 being selected in the back-end.

  • 
    $count = count(get_field('featured_developments'));
    // use this number for the count of rows
    if (have_rows('featured_developments')) {
      // etc....
    }
    
  • Hi John,

    Thanks for getting back to me, I tried that and didn’t work the “echo $count = 2” was correct so I used this code;

    <?php 
        $count = count( get_sub_field('featured_developments') );
        echo '<strong>The total is '. $count . '</strong></br><br>';
    	if( have_rows('featured_developments') == 1 ) {
    ?>
    	<?php while( have_rows('featured_developments') ): the_row(); ?>    
    	<?php
    	    $post_id = get_sub_field('development', false, false);
    	?>			
    
    	<div class="lg-col-12"></div>
    
    	<?php $count++; ?>    
    	<?php endwhile; ?>
    <?php
    	} elseif( have_rows('featured_developments') == 2 ) {
    		echo '2';
    ?>
    	<?php while( have_rows('featured_developments') ): the_row(); ?>    
    	<?php
    	    $post_id = get_sub_field('development', false, false);
    	?>			
    
    	<div class="lg-col-6 md-col-6"></div>
    
    	<?php $count++; ?>    
    	<?php endwhile; ?>
    <?php
    	} else{
    ?>
    		<?php while( have_rows('featured_developments') ): the_row(); ?>    
    	<?php
    	    $post_id = get_sub_field('development', false, false);
    	?>			
    
    		<div class="lg-col-4 md-col-4"></div>
    
    		<?php $count++; ?>    
    		<?php endwhile; ?>
    <?php
    	}
    ?>    
    <?php wp_reset_query(); ?>

    It outputted “lg-col-12”, when it should’ve outputted “lg-col-6”

  • In your case there is not a reason to increment count $count++ as $count already has the total number of rows in your repeater.

  • I removed that thanks, but still no change;

    <?php 
        $count = count( get_sub_field('featured_developments') );
        echo '<strong>The total is '. $count . '</strong></br><br>';
    	if( have_rows('featured_developments') == 1 ) {
    ?>
    	<?php while( have_rows('featured_developments') ): the_row(); ?>    
    	<?php
    	    $post_id = get_sub_field('development', false, false);
    	?>
    		<div class="lg-col-12">
    		</div>   
    	<?php endwhile; ?>
    <?php
    	} elseif( have_rows('featured_developments') == 2 ) {
    ?>
    	<?php while( have_rows('featured_developments') ): the_row(); ?>    
    	<?php
    	    $post_id = get_sub_field('development', false, false);
    	?>
    		<div class="lg-col-6 md-col-6">
    		</div>   
    	<?php endwhile; ?>
    <?php
    	} else{
    ?>
    	<?php while( have_rows('featured_developments') ): the_row(); ?>    
    	<?php
    	    $post_id = get_sub_field('development', false, false);
    	?>
    		<div class="lg-col-4 md-col-4">
    		</div>  
    	<?php endwhile; ?>
    <?php
    	}
    ?>    
    <?php wp_reset_query(); ?>
  • 
    $count = count(get_field('featured_developments'));
    if (have_rows('featured_developments')) {
    	$class = 'lg-col-4 md-col-4';
    	if ($count == 1) {
    		$class = 'lg-col-12';
    	}
    	if ($count == 2) {
    		$class = 'lg-col-6 md-col-6';
    	}
    	while (have_rows('featured_developments')) {
    		the_row();
    		?><div class="<?php echo $class; ?>"><?php echo $class; ?></div><?php 
    	}
    }
    
  • Perfect thanks John, I can see now where I was going wrong 🙂

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

The topic ‘Count total number of post objects’ is closed to new replies.