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.

When certain conditions are met (e.g. post vs. page or a category is tagged, etc) I would like to load an additional CSS file. This file would, in theory, change link colors. Or perhaps change the background color. Cosmetic changes. My thinking was to add code to functions.php, however, I don't think this is actually adding the CSS page. Even (even though right now there is no conditional, it should always just add this new page).

Help?

 /*
  * register with hook 'wp_print_styles'
  */
  add_action('wp_print_styles', 'add_my_stylesheet');

/*
 * Enqueue style-file, if it exists.
 */

function add_my_stylesheet() {
    $myStyleUrl = WP_THEME_URL . '/newStyle.css';
    $myStyleFile = WP_THEME_URL . '/newStyle.css';
    if ( file_exists($myStyleFile) ) {
        wp_register_style('myStyleSheets', $myStyleUrl);
        wp_enqueue_style( 'myStyleSheets');
    }

}

Thank you

share|improve this question
1  
I think wp_print_styles is administration-page hook. Anyway why don't you handle this in the header.php file using conditional tags? –  onetrickpony Jan 18 '11 at 3:10
    
@Ambitious Amoeba this seems to be modified example from Codex so likely hook is correct. Also queues are very convenient when you have multiple possible combinations and/or want to enforce specific loading order (matters for CSS or JS either). Doing that in template is pain. –  Rarst Jan 18 '11 at 7:14

2 Answers 2

up vote 0 down vote accepted

It seems like copy/paste gone wrong - both your variables point to same URL link and so file_exists() fails because it expects local path.

Change $myStyleFile to local path and it should work.

share|improve this answer
    
Eg. get_bloginfo( 'stylesheet_directory' ) . /newStyle.css .. –  t31os Jan 18 '11 at 8:40

Use WordPress Conditional tags in your functions.php file to register and enqueue on specific pages in your theme: http://codex.wordpress.org/Conditional_Tags

    // script manager template for registering and enqueuing files
    function childtheme_script_manager() {
        // register custom style on your specific page
        // use WordPress conditionals: 
        if (is_page('your-page-slug')) {
            wp_register_style('custom-css', get_stylesheet_directory_uri() . '/custom-style.css');
            wp_enqueue_style ('custom-css-css');
        }
    }
    add_action('wp_enqueue_scripts', 'childtheme_script_manager');
share|improve this answer

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.