Support

Account

Home Forums Add-ons Flexible Content Field Flexible – Add code to last row entry

Solving

Flexible – Add code to last row entry

  • After searching around I have a counter to count the number of entries of rows of a flexible content. The counter will insert a div before the number of entries. What I am trying to resolve is a way to add code after the last row entry?

    <?php                 
        if( have_rows('cm_content') ):                   
    
            $counter = 0;
            while ( have_rows('cm_content') ) : the_row();                
                
                $counter++;
                if($counter == 9 ){
                        echo '<div class="col-xs-12">break</div>';
                     }                             
                if( get_row_layout() == 'iu_image_url' ):                                 
                        
                        $image = get_sub_field('iu_image');
                        $imageurl = get_sub_field('iu_url');
    
                        echo '
                                <div class="col-xs-6 col-sm-3">
                                    <div class="small_image_box">
                                        <a href="' . $imageurl . '" target="_blank">
                                            <img src="' . $image . '" alt="' . '" class="img-responsive center-block" />
                                        </a>
                                    </div>
                                </div>
                            ';
                elseif( get_row_layout() == 'ilm_image_media' ): 
    
                    $image = get_sub_field('ilm_image');
                    $imageurl = get_sub_field('ilm_url');                
                        
                    echo '
                            <div class="col-xs-6 col-sm-3">
                                <div class="small_image_box">
                                    <a href="' . $imageurl . '" target="_blank">
                                        <img src="' . $image . '" alt="' . '" class="img-responsive center-block" />
                                    </a>
                                </div>
                            </div>
                        ';  
                endif;
                                        
            endwhile;
        else :
        endif;
    ?>
  • I was able to solve this by finding the total amount of rows in your field by using get_field_object.

    In your code, after if( have_rows('cm_content') ): try this:

    $field_object = get_field_object('cm_content');
    $total_rows = count($field_object['value']);
    

    Then you can test for it with an if statement:

    if($counter == $total_rows ){
    // this is the last one!
    }
    

    Cheers,

    Andre

  • Hi agagnon,
    This worked for me too.
    Thanks,
    joe

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

The topic ‘Flexible – Add code to last row entry’ is closed to new replies.