Search code examples
phpcsswordpressstylesheet

WordPress enqueue stylesheet not working


I'm new to working with PHP in WordPress and I was hoping to add a stylesheet to a specific page.

In functions.php I made a function that I thought would add my stylesheet called 'testPost.css' if the page name is 'test2'. In this function I also added an alert box to test if the function works at all.

When I click on 'page2' I do see my alert box but I do not see my new styles. I printed get_stylesheet_uri() in my alert box and see that it is pointing to "localhost/wp-content/themes/twentythirteen/style.css". This is not what I wanted; I expected to link to my 'testPost.css' instead. Does anyone know what I'm doing wrong and the correct way to achieve what I want?

functions.php

function lindsay_script(){
    if(is_page('test2')) 
    {   
        $message = get_stylesheet_uri();
        echo "<script type='text/javascript'>alert('$message');</script>";

        wp_enqueue_style( 'twentythirteen-testPost', get_stylesheet_uri().'/testPost.css' );
    }
}
add_action('wp_enqueue_scripts', 'lindsay_script');

Solution

  • get_stylesheet_uri points to style.css.

    Assuming that the CSS file you made is in the same directory as style.css, replace get_stylesheet_uri with get_stylesheet_directory_uri and you should be good to go.