6

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 );
3
  • I don't think is_tax() is initialized on init yet. Try changing the action tag to wp Commented Feb 21, 2011 at 17:57
  • That did the trick! Unfortunately I can't seem to located the "Accepted" button, is there a certain rep needed to mark something as correct? Commented Feb 21, 2011 at 18:38
  • 1
    You cannot accept a comment. You can a) ask One Trick Pony to re-post it as an answer or b) accept another correct answer.
    – Michal Mau
    Commented Feb 21, 2011 at 22:46

1 Answer 1

4

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.

2
  • That's correct. My code example was a bit misleading in that it didn't actually have the calls for the JavaScript, just the CSS. I've updated the example to better fit the original question. Commented Feb 22, 2011 at 15:09
  • Actually, it's fine to enqueue styles on the wp_enqueue_scripts hook as well. Same basic reasoning.
    – Otto
    Commented Feb 22, 2011 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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