Support

Account

Home Forums Add-ons Repeater Field how do you reverse the order of repeater rows?

Unread

how do you reverse the order of repeater rows?

  • I’m trying to sort the rows in the while so that they are 3,2,1 instead of 1,2,3. Would be dope if it could be sorted by $week as it returns a numerical value. I read the ACF page on sorting rows but given I already have some custom code in here to exclude the last row in the while I am not sure how to merge it all.

    function smile_gallery( $atts ) {
        if( have_rows('smile_gallery', 3045) ):
    
        $i = 1;
    
        $html_out = '';
    
        $html_out .= '<div class="smile-container">';
    
            // vars
            $rows = get_field('smile_gallery', 3045);
            $end_row = end($rows);
            $recent = $end_row['smile_week'];
            $count = count( get_field('smile_gallery', 3045) );
            
            $html_out .= '<div class="col-md-6">';
                $html_out .= '<div class="smile-recent">';
                    $html_out .= $recent;
                $html_out .= '</div>';
            $html_out .= '</div>';
    
            $html_out .= '<div class="col-md-6">';
                $html_out .= '<div class="smile-thumbs">';
    
                while( have_rows('smile_gallery', 3045) ): the_row();
    
                // vars
                $week = get_sub_field('smile_week', 3045);
                $img = get_sub_field('smile_img', 3045);
                $caption = get_sub_field('smile_caption', 3045);
    
                // Do stuff with each post here
                if( $i < $count) :
    
                    // vars
                    $order = array();
                    
                    $html_out .= '<div class="smile-thumb-container">';
                        $html_out .= '<div class="smile-thumb-img" style="background: url('. $img .') no-repeat center top; background-size: cover;">';
                        $html_out .= '</div>'; // smile thumb img
                        $html_out .= '<a class="smile-thumb-week" href="' . $img . '" data-lightbox="smile-set" ';
                        if ($caption) :
                            $html_out .= 'data-title="' . 'Week ' . $week . ' - ' . $caption . '"';
                        endif;
                        $html_out .= '><h6>Week ' . $week . '</h6></a>'; // smile thumb week  
                    $html_out .= '</div>'; // smile thumb container
                endif;
    
                $i++;
    
                endwhile;
    
                $html_out .= '</div>';
            $html_out .= '</div>';
    
        $html_out .= '</div>'; // smile container
    
        endif;
    
        return $html_out;
    }
    add_shortcode( 'show_smiles', 'smile_gallery' );
Viewing 1 post (of 1 total)

The topic ‘how do you reverse the order of repeater rows?’ is closed to new replies.