Home › Forums › Add-ons › Flexible Content Field › Shortcode + Flexible Content
Hi Guys,
I’ve been trying to figure out something for a couple of days now, and would really appreciate some help as I’m very much pushing the limits of my knowledge.
In summary I have a a number of layout elements which can be created via the flexible content layouts, ie horizontal slider, masonry gallery, image with text overlaid. The idea is allow for limitless creation of these layouts, which can then be inserted into post content using shortcodes.
I’ve managed some success so far, but the trouble is in how do I extract multiple layouts of the same layout type for use in a shortcode. I.e If I created 3 horizontal galleries in my flexible layout, how do I specify number 2 or 3 gallery?
Here’s what I have so far:
add_shortcode('slideshow', 'horizontalslider');
function horizontalslider($atts, $content = null) {
extract(shortcode_atts(array(
), $atts));
if( have_rows('layout_element_inserter') ):
while( have_rows('layout_element_inserter') ): the_row();
if( get_row_layout() == 'horizontal_image_slider_layout' ):
$out1 = '<div class="owl-gallery owl-carousel owl-theme">';
while(has_sub_field('horizontal_image_slider')):
$image = wp_get_attachment_image_src( get_sub_field('slider_image'), 'full' );
$imageurl = $image[0];
$output .='<div class="item"><img src="'.$imageurl.'"/></div>';
endwhile;
$out3 ='</div>';
endif; endwhile; endif;
return $out1 . $output . $out3;
}
add_shortcode('horislider', 'horizontalslider');
Hi @eli
You could add the number as a value in the shortcode [shortcode number=2]
and then in the shortcode function instead of doing a have_rows loop you can save the entire field to a variable and fetch the correct row based on the number.
$flexible_field = get_field('layout_element_inserter');
You’d have to of course look for the correct index of the specific layout type and not just grab the index right of the array.
This entire workflow seems a little backwards to me tho.. Why not just have the flexible field on the pages and create all the content right there in the order you want it. If you like you can even completely ditch the regular editor and just use a flexible field layout with an editor as a substitute.
You could achieve the same front end result or even better since you can also wrap editor content in its own html.
You must be logged in to reply to this topic.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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.