Support

Account

Home Forums Add-ons Flexible Content Field Conditionally loading JavaScript based on flexible content in the page

Solved

Conditionally loading JavaScript based on flexible content in the page

  • Hi,

    I’m using acf and flexible content to propose different type of row for create a page (slider, gallery, texts, …) and i want to load on front some JS for each of this type but only if if used on the page.

    Thanks for your help!
    Simon

    • RemSEO

    • October 21, 2019 at 9:49 pm

    Hi Simon,

    Looks like you’ll have to use the get_row_layout() function.
    Here is the ACF documentation: https://www.advancedcustomfields.com/resources/get_row_layout/

    You could have something like this :

    EDIT: use “wp_enqueue_script()” to add your scripts and not “get_template_part()” function as i said in my first reply.

    if(have_rows('flexible_field_name')):
    while(have_rows('flexible_field_name')): the_row();
      
      //Don't forget to get your actual layout name
      if( get_row_layout() == 'slider' ):
        wp_register_script( 'unique_name', get_stylesheet_directory_uri() . '/js/your_file.js');
        wp_enqueue_script( 'unique_name' );
      endif;
    
      if( get_row_layout() == 'gallery' ):
        wp_register_script( 'unique_name', get_stylesheet_directory_uri() . '/js/your_file.js');
        wp_enqueue_script( 'unique_name' );
      endif;
    
    endwhile;
    endif;

    Let me know if it helps,

    RemSEO.

  • Thanks a lot.

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.