Support

Account

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

Solving

Flexible – Add code to last row entry

    • diadem

    • October 7, 2014 at 12:25 pm

    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

    • josephd

    • February 13, 2015 at 1:47 pm

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

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.