0

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

2
  • 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? Commented Jan 18, 2011 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
    Commented Jan 18, 2011 at 7:14

2 Answers 2

0

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.

1
  • Eg. get_bloginfo( 'stylesheet_directory' ) . /newStyle.css ..
    – t31os
    Commented Jan 18, 2011 at 8:40
0

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');

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.