Home › Forums › Add-ons › Flexible Content Field › Need Help Getting Started with Flexible Content
Hi there,
Long time fan of ACF/Repeater combo. Just transitioned to a new agency, and we’ve purchased ACF Pro and I am now utilizing Flexible Content for the first time to output and sort an Isotope layout. From the documentation, I’m not getting the setup fully (doc page didn’t have the referred screen shots to show setup of the field group).
Once I have an example, I can expand and adjust from that, but haven’t been able to get it working yet.
Here’s my field group:
Field Label: Home Content Block
Field Name: home_content
Filed Type: Flexible Content
Layout:
Label: 1×1 Nav
Name :1x1_nav
Display: Table
1. Label: Image
Name: img
Type: Image
2. Label: Alt Text
Name: alt
Type: Text
3. Label: URL
Name: url
Type: Text
4. Label: Display Categories
Name: display_cat
Type: Checkbox
Then my initial PHP code:
<?php
// check if the flexible content field has rows of data
if( have_rows(‘home_content’) ):
// loop through the rows of data
while ( have_rows(‘home_content’) ) : the_row();
// check current row layout
if( get_row_layout() == ‘1x1_nav’ ):
// check if the nested repeater field has rows of data
if( have_rows(‘1x1_nav’) ):
echo ‘<div class=”item home isotopeNav”>’;
// loop through the rows of data
while ( have_rows(‘1x1_nav’) ) : the_row();
$img = get_sub_field(‘img’);
$url = get_sub_field(‘url’);
$alt = get_sub_field(‘alt’);
$displayCat = get_sub_field(‘display_cat’);
endwhile;
echo ‘</div>’;
endif;
endif;
endwhile;
else :
// no layouts found
endif;
?>
I don’t fully understand what field/variables I’m supposed to be checking at “// check if the nested repeater field has rows of data” and deeper.
Any help would be appreciated.
Thanks,
Ryan
I am in the same boat here. I just upgraded to Pro and am trying to use Flexible Content for the first time and not having any luck. Eliot, is there anyway you can post the exact code you used from the demo video? I think that would lend a lot of help with syntax and how to query everything properly.
I got some stuff working after playing around for awhile. First, here’s my current test setup—the flexible content has two different image uploads right now, one that spits out an image only, and another that spits out an image wrapped in some HTML that looks like a browser window.
And here’s the code I’m using to have it spit it all out:
<?php if( have_rows('project_content') ): ?>
<?php while( have_rows('project_content') ): the_row(); ?>
<?php // Normal Image Content Block
if( get_row_layout() == 'content_image' ): ?>
<?php // Vars
$contentImage = get_sub_field('image');
echo '<figure><img src="'. $contentImage['url'] .'" alt="'. $contentImage['alt'] .'" /></figure>'
?>
<?php endif; ?>
<?php // Browser Image Content Block
if( get_row_layout() == 'content_image_browser' ): ?>
<?php // Vars
$browserImage = get_sub_field('image_browser');
$browserLabel = get_sub_field('image_browser_label');
echo '<figure class="browser"><div><span></span><span></span><span></span></div><img src="'. $browserImage['url'] .'" alt="'. $browserImage['alt'] .'" /><figcaption>'. $browserLabel . '</figcaption></figure>'
?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Hope that helps you out! It did the trick for me. Now I’m just going to be following that same template and adding other content block types.
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!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 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.