0

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?

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.