Enqueue Style for a page/pages only

it is causing problem because i did not do it before. I am trying to enqueue style or may be scripts depending on page. But it not working. Here is the code:

add_action('init', 'my_enqueue_styles');

function my_enqueue_styles(){
    if(is_page('Add Event')){ // also tried slug, page id and wp_reset_query(); bot not worked

    wp_deregister_style( 'jquery-ui-custom-flick' );
    wp_register_style( 'jquery-ui-custom-flick', get_bloginfo('template_directory') .'/styles/jquery.ui/ui.custom.flick.css');
    wp_enqueue_style( 'jquery-ui-custom-flick' );
    }

}

I am not doing the conditional right. The script works without the conditional.

Read More

Thanks!

RESOLVED:

The problem was with the init action hook. the conditional is_page() is false when init is called. After adding the style to the hook wp_print_styles it worked perfectly.

Related posts

Leave a Reply

1 comment

  1. It should work like this.

    Sidenotes: I don’t know why you deregister a stylesheet and register it again. Also: get_bloginfo('template_directory') is now replaced by get_template_directory_uri(). Third: Are your folders really named with dots in between? Maybe this causes problems. And maybe your ui stylesheet is an dependency of the main jquery ui stylesheet.

    You should also start accepting answers on your questions. Your “accept rate” of 67% will people hold back from answering your Qs.

    function wpse_16487_enqueue_styles()
    {
        if ( is_page('Add Event') ) // also tried slug, page id and wp_reset_query(); bot not worked
        { 
            wp_register_style( 'jquery-ui-custom-flick', get_template_directory_uri().'/styles/jquery-ui/ui-custom-flick.css', 'jquery-ui' );
            wp_enqueue_style( 'jquery-ui-custom-flick' );
        }
    }
    add_action( 'wp_print_styles', 'wpse_16487_enqueue_styles' );
    

    If something’s not working, you should start debugging your wp query: echo '<pre>'; print_r($GLOBALS['wp_query']); echo '</pre>'; and check for the page name/slug to register it.