Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Before I get started, I want to acknowledge that there are some really good articles that are targeted at how to conditionally enqueue scripts based on page/post content.

These are great, but they're much more advanced than what I want to do.

I feel like the answer is using the built-in is_page() function (codex), but when I try to use it the site breaks, or just doesn't work.

I think I'm just executing the conditional logic in the wrong spot.

Here's what I've tried to add to my functions.php:

function register_more_stylesheets() {
    wp_register_style( 'stylesheet_name', get_stylesheet_directory_uri() . '/stylesheet.css' );
}
function add_my_stylesheet() {
    if ( is_page('products-services') ) // using page slug
    wp_enqueue_style( 'stylesheet_name' );  // no brackets needed for one line and no else
}
add_action( 'init', 'register_more_stylesheets' ); // should I use wp_print_styles hook instead?
add_action( 'wp_enqueue_scripts', 'add_my_stylesheet' );

When I remove the conditional part of it, the stylesheet is successfully enqueued so I know that works.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

I copy pasted your code into my dev environment, changed nothing but the page name, and it works just fine. Are you sure that it's not being enqueued and you just have it pointed wrong or that the page is not named incorrectly or something?

share|improve this answer
    
Thanks, it does work! I don't know why it wasn't working before. Stranger things have happened I guess. Hopefully this will be of use to someone else. –  Evan Mattson Jan 17 '12 at 0:19
add comment

What you've got is right--you're just hooking it to the wrong action. Try the "wp" action instead of "init". When init runs, the is_page() won't work properly yet.

EDIT: I'm wrong. I thought you had add_my_stylesheet hooked to init, but you don't. is_page should work from the enqueue_scripts action. Sorry...carry on...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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