How to load CSS ( wp_enqueue_style() ) if sub_field of flexible fields used in page template

37 Views Asked by At

I need to load CSS only if sub_field of flexible fields group is in use on a page template. For example:

$flexable = get_field('flexable_fields');
foreach( $flexable as $item ){
if($item['title']){
wp_enqueue_style('title-styles', get_template_directory_uri() . '/css/style.css', array(), filemtime(get_template_directory() . '/css/title-style.css'), false);
}
}

Which hook I need to use to implement this, "init" or something else? Thanks

1

There are 1 best solutions below

0
Faisal On

The most suitable hook to use would be wp_enqueue_scripts.

add_action( 'wp_enqueue_scripts', function() {
    $flexable = get_field('flexable_fields');
    foreach( $flexable as $item ){
        if($item['title']){
            wp_enqueue_style('title-styles', get_template_directory_uri() . '/css/style.css', array(), filemtime(get_template_directory() . '/css/title-style.css'), false);
        }
    }
});