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

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

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

The topic ‘Conditionally loading JavaScript based on flexible content in the page’ is closed to new replies.