I’m looking to add some scripts and styles to particular taxonomy page. For example, I want to have a banner rotator only appear for posts that fall under the “issue” custom taxonomy. I’ve tried using the is_tax(‘issue’) to control when the scripts and styles are enqueued but that doesn’t seem to be working. Here’s a sample of my functions.php file:
function init_customizations() {
if (is_tax('issue')) {
wp_register_script('rotator_scripts',get_bloginfo('template_directory').'/includes/issue-rotator.js', array(), '1.0.0' );
wp_enqueue_script('rotator_scripts');
wp_register_style('rotator_styles',get_bloginfo('template_directory').'/includes/issue-rotator.css', array(), '1.0.0', 'screen');
wp_enqueue_style('rotator_styles');
}
}
add_action( 'init', 'init_customizations', 0 );
This doesn’t actually write anything to the header so I suspect I may be calling it incorrectly.
Update
Here is the final code with the correct answer applied to it:
function init_customizations() {
if (is_tax('issue')) {
wp_register_script('rotator_scripts',get_bloginfo('template_directory').'/includes/issue-rotator.js', array(), '1.0.0' );
wp_enqueue_script('rotator_scripts');
wp_register_style('rotator_styles',get_bloginfo('template_directory').'/includes/issue-rotator.css', array(), '1.0.0', 'screen');
wp_enqueue_style('rotator_styles');
}
}
add_action( 'wp_enqueue_scripts', 'init_customizations', 0 );
For a function that enqueues scripts, the action hook you use should actually be “wp_enqueue_scripts” for the front of the site, and “admin_enqueue_scripts” for the admin side of things. This is the proper time to enqueue scripts.
While you can technically do it anytime before wp_head, this is the best place because it’s pretty much the last possible chance to do it, thus ensuring that everything else that can be done before script output has been done, making your logic all work properly.