Support

Account

Home Forums Add-ons Flexible Content Field Random Flexible Content Field Results Ordering

Solved

Random Flexible Content Field Results Ordering

  • Hi,

    I’m trying to ‘shuffle’ the results in the flexible content field so that they appear in a random order. I know this is possible with the repeater using a foreach loop (http://support.advancedcustomfields.com/forums/topic/random-repeater/), however I’ve not been able to get the flexible content field to display results with a foreach correctly (I’m not sure where I’m going wrong!).

    I’m getting the results for the current flexible content field using the below:

    
    <?php if(get_field("slideshow")): ?>
    
        <div>    
                    
            <?php while(has_sub_field("slideshow")): ?>
             
                <?php if(get_row_layout() == "image"): ?>
                
                    <!-- Image Layout Script -->
             
                <?php elseif(get_row_layout() == "youtube"): ?>
                
                    <!-- YouTube Layout Script -->
                
                <?php elseif(get_row_layout() == "vimeo"): ?>
                
                    <!-- Vimeo Layout Script -->
                
                <?php endif; ?>
             
            <?php endwhile; ?>
        
        </div>
     
    <?php endif; ?>
    

    Is there any options for ordering the FCF using the get_field method? Or is there a way of using a foreach more effectively for a flexible content field?

    Thanks in advance!!
    Steve

  • Hi @Steve

    You will need to use a foreach loop to iterate over the flexible content layouts.
    The code will look much the same as the repeater fiedl example, exept you will need to look at the ‘acf_fc_layout’ value instead of get_row_layout().

    When you loop through the rows, just print_r the $row to see what data is a available to you

    Thanks
    E

  • Thanks Elliot, that’s a great help. I’ll have a look at this and get back to you with my results!

    Cheers,
    Steve

  • I used this code for sort my flexible content. Basic is the same code for repeater.

    <?php
        $acontecimentos = get_field('acontecimentos');
        $date = [];
    
        //Reorder
        foreach($acontecimentos as $key => $row) {
            $date[$key] = $row['data'];
        }
    
        array_multisort($date, SORT_ASC, $acontecimentos);
    
        foreach($acontecimentos as $row) {
            if($row['acf_fc_layout'] == 'galeria'):
                //code
            endif;
        }
    ?>

    After array_multisort you can use shuffle http://us3.php.net/manual/en/function.shuffle.php

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

The topic ‘Random Flexible Content Field Results Ordering’ is closed to new replies.