Support

Account

Home Forums Add-ons Flexible Content Field Count taxonomy rows in flexible content

Solving

Count taxonomy rows in flexible content

  • I have created some flexible content and all is fine with one exception. I am having a hard time counting the number or repeater rows within one of the layouts. The same code pattern works fine outside of a flexible content field. I have tried both get_field('product_category_row') and get_sub_field('product_category_row')


    if( have_rows('flexible_content') ): ?>

    <?php
    // loop through the rows of data
    while ( have_rows('flexible_content') ) : the_row();

    if( get_row_layout() == 'editor' ): ?>
    <section class="flex-editor">
    <?php the_sub_field('wysiwyg_editor'); ?>

    </section>
    <?php
    elseif( get_row_layout() == 'product_categories' ):?>

    <section class="flex-product-category-row">

    <?php

    if( have_rows('product_category_row') ):

    $count= count(get_field('product_category_row'));

    if ($count == 1 ){
    $class = "full-width";
    } elseif ( $count == 2 ) {
    $class = "one-half";
    } elseif ( $count == 3) {
    $class = "one-third";
    } elseif ( $count == 4) {
    $class = "one-fourth";
    } else {
    $class = "one-fifth";
    }

    // loop through the rows of data
    while ( have_rows('product_category_row') ) : the_row();

    $terms= get_sub_field('product_category_select');

    if( $terms ): ?>

    <div class="<?php echo $class . ' '. $count; ?>">

    <h2><?php echo $terms->name; ?></h2>

    </div>
    <?php endif;
    endwhile; ?>

    </section>
    <?php

    else :

    // no rows found

    endif;

    endif;

    endwhile;

    else :

    // no layouts found

    endif;

    ?>

  • Hi @mantismamita

    Just to be safe try this:

    
    $repeater = get_field('product_category_row');
    $count = count($repeater);
    
  • Hey, I am having this same problem.
    I can count repeater rows normally. But if there is a repeater field inside the Flexible Content it won’t count.

    I have tired “get_field” and “get_sub_field” using the code by @jonathan but doesn’t count.

    Any luck @mantismamita ?
    Thanks

  • Hi @fazedesigns

    Could you post your entire code for this so I can have a look?

  • Hi @jonathan, this is what I have:

    <?php if( have_rows('section') ):
    
        while ( have_rows('section') ) : the_row();
    		
    		// Repeater Block
            if ( get_row_layout() == 'content_block_repeater' ):
    			
    			// Repeater Field
    			if( have_rows('repeater') ) :
    			$counter = get_sub_field('repeater');
    			//$counter = get_field('repeater');
    			echo '<div class="repeater_block">';
    				// Loop
    				while ( have_rows('repeater') ) : the_row();
    					$h3 = get_sub_field('repeater_h3');
    					$em = get_sub_field('repeater_em');
    					$count = count( $counter );
    					if ( $count == 3 ) {
    						$span = 'span33';
    					} elseif ( $count == 2 ) {
    						$span = 'span50';
    					} else {
    						$span = 'span100';
    					}
    					// Output
    					echo '<div class="'. $span .'">';
    						echo '<h3>'. $h3 .'</h3>';
    						echo '<em>'. $em .'</em>';
    					echo '</div>';
    					// End Output
    				endwhile;
    				// End Loop
    			echo '</div>';
    			endif;
    		
    		// Text Block
            elseif ( get_row_layout() == 'content_block_text' ): 
            
            	// get_template_part( '/_content/text' );
            
            else :
            	
            	// No Content
     
            endif;
     
        endwhile;
     
    endif; ?>
  • Hi @jonathan, I found the solution.
    Im not sure why this would break the count();.
    Do you know why?

    This is what I had:

    if ( have_rows('repeater') ) :
    	$counter = get_sub_field('repeater');
    	// $counter = get_field('repeater'); if not in Flexible Content
    	while ( have_rows('repeater') ) : the_row();
    		$count = count( $counter );
    		echo $count;
    		// HTML goes here
    	endwhile;
    endif;

    And this is what works:

    $counter = get_sub_field('repeater');
    // $counter = get_field('repeater'); if not in Flexible Content
    if ( have_rows('repeater') ) :
    	while ( have_rows('repeater') ) : the_row();
    		$count = count( $counter );
    		echo $count;
    		// HTML goes here
    	endwhile;
    endif;
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.