Home Forums Add-ons Flexible Content Field Flexible Content Layout Conditional


Flexible Content Layout Conditional

    • jmccall

    • February 21, 2019 at 2:55 am

    What’s the simplest conditional for testing if a specific flex content layout is being used on a page? Do I have to loop through each layout to do that?

    For example, I have a flex content field called “page blocks” with five different layouts. I need to know if one specific layout (“slider”) exists so that I can enqueue the appropriate JS in the template footer. What’s the best way to do this?


  • There are a couple of ways to do what you what. The first is to enqueue the script in the footer when you actually output the “slider”. This will work, and you’ll only get one instance of the script no matter how many times it’s called.

    simple example:

    while (have_rows('flex-field')) {
      if (get_row_layout()) == 'slider') {
        // enqueue slider script with $in_footer set to true

    Another way is to get the flex field using get_post_meta(). When you get the flex field this way it will return an array containing a list of layout names.

    $layouts = get_post_meta($post_id, 'flex-field-name', true);
    if (is_array($layouts) && in_array('slider', $layouts)) {
      // enqueue slider script
    • jmccall

    • February 22, 2019 at 12:43 am

    Thanks for both those solutions John! It didn’t even occur to me that I could enqueue the script from within the loop itself.

    • lstreng

    • July 31, 2019 at 11:08 pm

    @hube2 so I have a really silly question to your answer… How are you enqueuing the script inside the loop itself?

    I’m actually trying to do this same thing. Drupal is nice in that I can attach libraries in any template I want and if I can figure out the same for WordPress that would be fantastic!

    • jmccall

    • July 31, 2019 at 11:18 pm

    @lstreng – this is how I did it:

    <?php // Loop through the repeater rows
        	if (have_rows('info_block')) :
                    wp_enqueue_script('slick-home'); // js triggers for slider
        			while ( have_rows('info_block') ) : the_row();
    • lstreng

    • July 31, 2019 at 11:22 pm

    Hah, well that’s simple enough – never enqueue’d in any other file other than functions. Thanks 😛

  • When you use wp_register_script() set $in_footer to true.

    Or alternately, just call wp_enqueue_script() with all of the arguments including $in_footer in your loop.

    I’m using slick as well and in my template part for each layout I have something like this

    wp_enqueue_script('slick', get_template_directory_uri().'/assets/vendor/slick/slick.min.js', array('jquery'), '1.8.1', true);
    wp_enqueue_script('slick-init', get_template_directory_uri().'/js/slick-init.js', array('jquery', 'slick'), '1.0.0', true);

    My slick init script actually is pretty complicated and I give each type of slider a different class and then initialize each class. This way I have just a single initialization script to load and it remains the same on all pages so that it can be cached by the browser and does not cause other caching issues.

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

You must be logged in to reply to this topic.

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.