Support

Account

Home Forums Add-ons Flexible Content Field Shortcode + Flexible Content

Helping

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.

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

The topic ‘Shortcode + Flexible Content’ is closed to new replies.