Question

I am using Masonry (included in WP 3.9) in my childe theme.

I followed this tutorial.

In their code for functions.php they use conditional statement:

if ( ! is_admin() ) :
function slug_scripts_masonry() {
    wp_enqueue_script('masonry');
    wp_enqueue_style('masonry’, get_template_directory_uri().'/css/’);
}
add_action( 'wp_enqueue_scripts', 'slug_scripts_masonry' );
endif; //! is_admin()

The ! is_admin seems to work, because when I look up the source, I can't find anything. However, my goal is to only let this function run when we are an a taxonomy page. However, changing it to:

if ( is_tax() ) :
    function slug_scripts_masonry() {
        wp_enqueue_script('masonry');
        wp_enqueue_style('masonry’, get_template_directory_uri().'/css/’);
    }
    add_action( 'wp_enqueue_scripts', 'slug_scripts_masonry' );
    endif; 

does not work! It's including nowhere anymore! What am I doing wrong?

Was it helpful?

Solution

Move the is_tax() conditional inside the slug_scripts_masonry function.

function slug_scripts_masonry() {
    if ( is_tax() ) {
        wp_enqueue_script( 'masonry' );
        wp_enqueue_style( 'masonry', get_template_directory_uri().'/css/' );
    }
}

add_action( 'wp_enqueue_scripts', 'slug_scripts_masonry' );

Because Wordpress has not yet executed the code which determines the result of is_tax() it will return false on all pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top