Home Forums Add-ons Repeater Field how would you create multiple galleries with ACF?


how would you create multiple galleries with ACF?

  • I need to create an element.

    On my homepage I plan on having multiple galleries, there can be one or ten, I want this to be flexible. I was hoping to use ACF and repeaters to create as many as I’d want, but I wanted to output each one in a shortcode so I can place them wherever I want in my design.

    The shortcode would have an ID that would match somehow with the gallery made with ACF. I’m just a bit unsure if this is even possible or not, and if so I am not sure how to take a field that holds the ID and output it in a shortcode.

    I was thinking it would look like [gallery id="1"] [gallery id="2"] [gallery id="3"], etc.

    Could use some help on this. Thanks.

  • I would probably create a custom post type and each post in the custom post type would have 1 gallery. This would give you an “ID” that could be used in a shortcode.

  • function mbgc_gallery_shortcode( $atts ) {
        // $post_id = $atts['id'];
        extract( shortcode_atts(
                'id' => '',
            ), $atts )
        $html_out = '123';
        // $post = get_post( $id );
        // if ( $post ) :
            if( have_rows('mbgc_gallery') ):
                $html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">';
                while( have_rows('mbgc_gallery') ): the_row(); 
                    // vars
                    $image = get_sub_field('mbgc_image');
                    $caption = get_sub_field('mbgc_caption');
                    $title = get_sub_field('mbgc_title');
                    $sub_title = get_sub_field('mbgc_sub_title');
                    if ( $image ) :         
                        $html_out .= '<div class="mbgc-gallery-item">';
                            if ( $caption ) : 
                                $html_out .= '<div class="mbgc-slide-caption">';
                                    $html_out .= '<h4>' . $caption . '</h4>';
                                    $html_out .= '<div class="mbgc-slide-titles">';
                                        $html_out .= '<h6>' . $title . '</h6>';
                                        $html_out .= '<h6>' . $sub_title . '</h6>';
                                    $html_out .= '</div>';
                                $html_out .= '</div>';
                            $html_out .= '<div class="mbgc-slide">';
                                $html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
                            $html_out .= '</div>';
                        $html_out .= '</div>';
                $html_out .= '</div>';
        // endif;
        return $html_out;
    add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode');

    This is probably out of the scope of what you can help with, but I am trying to write a function based on what you said. Creating a Custom Post Type. I can’t get this to work though. I tried [mbgc_gallery id=”401″] and it outputs nothing, only the test 123. I tried a var_dump and nothing neither. Am I using the repeater code correctly?

  • Inside of a function like this you need to include the post ID in acf function calls, example have_rows('mbgc_gallery', $post_id). You don’t need to include the ID when dealing with sub fields like get_sub_field('mbgc_image')

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

You must be logged in to reply to this topic.