• Resolved 14oangus

    (@14oangus)


    Hi Konrad,

    Thanks for the amazing plugin.

    The issue I’m facing has something to do with flexible content and dynamic preview which happens after upgrading to 0.9.0.9 from 0.9.0.7.

    Issue: The preview for each flexible row displays all rows. This only occurs on the admin pages but not on the frontend.

    Flexible Content Field
    Layout 1 (previews Layout 1 & 2; displays Layout 1 only on frontend)
    Layout 2 (previews Layout 1 & 2; displays Layout 2 only on frontend)

    Could you let me know what was changed and how I should edit my code accordingly?

    /flexible-content.php

    <?php
    if (have_rows('flexible_content_field')) :
    while (have_rows('content_groups')) :
    the_row();
    include('flexible-content/layout-1.php');
    include('flexible-content/layout-2.php');
    endwhile;
    endif;
    ?>

    /flexible-content/layout-1.php

    <?php if (get_row_layout() == 'layout_1') : ?>
    <div>
    Layout 1 HTML here
    </div>
    <?php endif; ?>

    Regards,
    Angus

    • This topic was modified 1 year, 6 months ago by 14oangus.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback, and sorry for the late answer, I was busy with the latest patch lately.

    What you describe is symptomatic of a bad Flexible Content layout configuration. It looks like you defined your layouts PHP templates to use the same global flexible-content.php, which means each layout retrieve the whole Flexible Content and display all layouts.

    I would recommend to use the following configuration:

    • Layout 1 render flexible-content/layout-1.php
    • Layout 2 render flexible-content/layout-2.php

    Here is a screenshot showing this configuration. Additionally, I would recommend to use has_flexible(): the_flexible() to automatically render the Flexible Content and the layouts templates on the front-end, as described in the documentation.

    Usage example:

    if(has_flexible('flexible_content_field_name')):

    the_flexible('flexible_content_field_name');

    endif;

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter 14oangus

    (@14oangus)

    Hi Konrad,

    Thanks for getting to me and for the continuous work on the plugin.

    I’d found that part of the docs before you mentioned it but it’s still not clear to me where the sample code should go and what it replaces.

    if(has_flexible('flexible_content_field_name')):

    the_flexible('flexible_content_field_name');

    endif;

    I tried to change the code for each layout use its own template file for rendering but couldn’t figure how to integrate the above sample code. Since my current flexible content field has lots of layouts under it, I thought I’d reach out again to clear this up before I make a bunch of changes that don’t work.

    Using the same example:
    flexible_content_field
    layout_1
    layout2

    Now I don’t need the old flexible-content.php file, right?
    So I assume I should start each layout template file with the above sample code. For example:

    layout-1.php

    if(has_flexible('flexible_content_field')):

    the_flexible('flexible_content_field');

    // should I get the layout's fields as before from here?
    if (get_row_layout() == 'layout_1') : ?>
    <div>
    Layout 1 HTML here
    </div>
    <?php endif;

    endif;

    I think I just need to know what each layout template file should look like.
    Feel free to let me know if you need more information about my setup.

    Thanks again,
    Angus

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Yes, I think there is a misundrstanding.

    When displaying the Flexible Content on your front-end page template, for example on page-my-page.php, instead of using the classic ACF Flexible Content markup, described in the ACF documentation:

    <?php get_header(); ?>

    <?php if(have_posts()): ?>
    <?php while(have_posts()): the_post(); ?>

    <h1><?php the_title(); ?></h1>

    <?php
    if(have_rows('my_flexible_content')):
    while(have_rows('my_flexible_content')): the_row();

    // Layout: Hero
    if(get_row_layout() === 'hero'):

    $text = get_sub_field('text');
    // ...

    // layout: Slider
    elseif(get_row_layout() === 'slider'):

    $text2 = get_sub_field('text2');
    // ...

    endif;

    endwhile;
    endif;
    ?>

    <?php endwhile; ?>
    <?php endif; ?>

    <?php get_footer(); ?>

    You can should use the following code instead:

    <?php get_header(); ?>

    <?php if(have_posts()): ?>
    <?php while(have_posts()): the_post(); ?>

    <h1><?php the_title(); ?></h1>

    <?php
    if(has_flexible('my_flexible_content')):
    the_flexible('my_flexible_content');
    endif;
    ?>

    <?php endwhile; ?>
    <?php endif; ?>

    <?php get_footer(); ?>

    The code has_flexible(): the_flexible() is a drop-in replacement for the have_rows(): the_row() logic, which automatically includes your layout PHP templates files, layout stylesheets/scripts…

    Here is a visual representation of what has_flexible(): the_flexible() is doing to help you understand:

    <?php get_header(); ?>

    <?php if(have_posts()): ?>
    <?php while(have_posts()): the_post(); ?>

    <h1><?php the_title(); ?></h1>

    <?php

    /**
    * visual representation of:
    *
    * if(has_flexible('my_flexible_content')):
    * the_flexible('my_flexible_content');
    * endif;
    *
    */

    if(have_rows('my_flexible_content')):
    while(have_rows('my_flexible_content')): the_row();

    // Layout: Hero
    if(get_row_layout() === 'hero'):

    // include('themes/my-theme/flexible-content/hero.php');
    // <script src="themes/my-theme/flexible-content/hero.js" />
    // <link href="themes/my-theme/flexible-content/hero.css" />

    // Layout: Slider
    elseif(get_row_layout() === 'slider'):

    // include('themes/my-theme/flexible-content/slider.php');
    // <script src="themes/my-theme/flexible-content/slider.js" />
    // <link href="themes/my-theme/flexible-content/slider.css" />

    // Layout: Cards
    elseif(get_row_layout() === 'cards'):

    // include('themes/my-theme/flexible-content/cards.php');
    // <script src="themes/my-theme/flexible-content/cards.js" />
    // <link href="themes/my-theme/flexible-content/cards.css" />

    endif;

    endwhile;
    endif;

    ?>

    <?php endwhile; ?>
    <?php endif; ?>

    <?php get_footer(); ?>

    As you can see, when you use has_flexible(): the_flexible() you don’t have to bother with multiple if/elseif, include layout files manually etc… as everything is automatically handled.

    In this example, a typical code for the hero.php layout would be:

    <div class="layout-hero">

    <h1>Hero</h1>
    <h3><?php the_sub_field('text'); ?></h3>

    </div>

    This is why has_flexible(): the_flexible() should not be included in your layout layout.php PHP files, unless you want to display a “Sub Flexible Content” (when using a “Flexible Content > Sub Flexible Content” setup), but this is an advanced usage I doubt you need it.

    Hope it helps!

    Regards.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Update: I updated the Dynamic Render documentation, it should be now more clear.

    Thread Starter 14oangus

    (@14oangus)

    Hi Konrad,

    Thanks a lot for the detailed explanation and for even bothering to update the docs. I really appreciate it.

    Most importantly, this all works great! I confirm the updated, cleaner code resolved my initial issue.

    Thanks again.

    Cheers,
    Angus

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Awesome! I’m glad to hear it now works as expected!

    If you enjoy this plugin and its support, feel free to submit a review. It always helps and it’s much appreciated 🙂

    Have a nice day, and Merry Christmas!

    Regards.

    Thread Starter 14oangus

    (@14oangus)

    Merry Christmas, Konrad!

    I will for sure submit a 5 star review once the service reopens after the holiday break.

    Thanks again for the on-point support.

    Angus

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘0.9.0.9 Flexible Content Dynamic Preview Question’ is closed to new replies.