Support

Account

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

Solved

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?

    Thanks.

  • 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')) {
      the_row();
      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.

Viewing 3 posts - 1 through 3 (of 3 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.