Home › Forums › Add-ons › Repeater Field › 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(
array(
'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');
var_dump($caption);
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>';
endif;
$html_out .= '<div class="mbgc-slide">';
$html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
endwhile;
$html_out .= '</div>';
endif;
// 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')
The topic ‘how would you create multiple galleries with ACF?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.