Support

Account

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

  • 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.