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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.