Home › Forums › Add-ons › Flexible Content Field › Can I use more than one layout field per page?
Update I removed all PHP code from inside the IF statement and adding multiple of the same layout sections/rows is working. Then I did test the switch statement and it works really well.
New Q: Can I use PHP require_once inside the if/switch statement to keep the code a little more manageable?
1. The goal, use Flexible Content as a semi-page builder. So example, I want to use the same layout content twice. To this point, I have successfully created 4 layouts (Slider, Cards, Dynamic Post, and Custom HTML). Each section successfully displays and is able to be reordered, however, if I try to add an additional section of the same type it will not output.
<php?
$row = have_rows('row');
if ( $row ) :
while ( $row ) : the_row();
if ( get_row_layout() == 'slider' ) :
endif;
if ( get_row_layout() == 'cards' ) :
endif;
if ( get_row_layout() == 'custom_block' ) :
endif;
if ( get_row_layout() == 'dynamic_post' ) :
endif;
endwhile;
endif;
?>
This only allows me to use each flexible layout once. Should I use some sort of foreach loop? Is there ACF documentation on using a foreach layout loop?
2. Off-topic question, would I see some improved performance by changing the IF statements to switch statements?
<?php
$row = have_rows('row');
if ( $row ) :
while ( $row ) : the_row();
switch ( get_row_layout() ) :
case 'slider':
// Slider PHP
break;
case 'cards':
// Card PHP
break;
case 'custom_block':
// Custom Block PHP
break;
case 'dynamic_post':
// Dynaic Post PHP
break;
endswitch;
endwhile;
endif;
?>
Hi @glenn
The foreach()
method is just like while()
method, it just have a different format. Also, while()
method should loop through all of your flexible content rows. Could you please share the JSON export file of your field group so I can test it out?
Regarding the switch()
method, the performance should be equal. It’s just a matter of preference. Please check this thread to learn more about it: http://stackoverflow.com/questions/10773047/which-is-faster-and-better-switch-case-or-if-else-if.
I hope this makes sense 🙂
Hey @acf-support,
I have narrowed down the issue to be this PHP code which is query the_post(). When this code is in my front-page.php it will display the HTML for my categories, but layout rows after will not output.
switch ( get_row_layout() ) :
case 'slider':
echo '<div class="slider">';
echo 'THE SLIDER';
echo '</div><!-- @end slider -->';
break;
case 'cards':
echo '<div class="cards">';
echo 'THE CARDS';
echo '</div><!-- @end cards -->';
break;
case 'custom_html':
echo '<div class="custom-html">';
echo 'THE CUSTOM HTML';
echo '</div><!-- @end custom html -->';
break;
case 'dynamic_post':
$section_container = get_sub_field('section_container');
$section_padding = get_sub_field('section_padding');
$section_background = get_sub_field('section_background');
$section_bg_image = get_sub_field('section_bg_image');
$section_bg_color = get_sub_field('section_bg_color');
$section_bg_color_stop = get_sub_field('section_bg_color_stop');
$color_transparency = get_sub_field('color_transparency');
$gradient_direction = get_sub_field('gradient_direction');
$section_title = get_sub_field('display_section_title');
$component_type = get_sub_field('content_style');
$content_type = get_sub_field('content_type');
$orderby = get_sub_field('order_by');
$number_of_posts = get_sub_field('number_of_post');
$categories = get_sub_field('category');
$name_of_post = get_sub_field('post');
$name_of_page = get_sub_field('page');
$slider_class = get_sub_field('slider_class');
if ( $content_type === 'category' ) :
foreach ( $categories as $category ) :
$cat_slug = $category->slug;
endforeach;
$args = array(
'posts_per_page' => (int)$number_of_posts,
'orderby' => $orderby,
'category_name' => $cat_slug
);
$query = new WP_query( $args );
switch ( $component_type ) :
case 'cards':
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
echo '<div class="card--quote">';
$imgSrc = get_field('image');
if ( !empty($imgSrc) ) :
$imgUrl = $imgSrc['url'];
$imgTitle = $imgSrc['title'];
$imgAlt = $imgSrc['alt'];
$caption = $imgSrc['caption'];
$imgSize = 'testimonial-thumb';
$imgThumb = $imgSrc[ 'sizes' ][ $imgSize ];
$imgWidth = $imgSrc['sizes'][ $imgSize . '-width' ];
$imgHeight = $imgSrc['sizes'][ $imgSize . '-height' ];
echo '<div class="card__image">';
echo '<img src="' . $imgUrl . '" alt="' . $imgAlt . '" width="' . $imgWidth . '" height="' . $imgHeight . '" />';
echo '</div>';
endif;
if ( get_field('quote') ) :
echo '<blockquote class="card__quote">';
echo '<p>';
the_field('quote');
echo '<cite>';
the_field('cite');
echo '</cite>';
echo '</p>';
echo '</blockquote>';
endif;
echo '</div><!-- @end card quote -->';
endwhile;
endif;
break;
Hey @acf-support,
I forget to reset the custom query. Once I added wp_reset_query(); after the if/while loop, everything worked.
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 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.