2

I'm new to WordPress. I'm including my file using

<link rel='stylesheet' href="<?php bloginfo("stylesheet_url")?>"/>

But if i enqueue my css file in functions.php it is not working. Why? Where is my mistake?

<?php wp_enqueue_style('style',get_template_directory_uri())?>

What is the solution for this?

2 Answers 2

3

You are calling the function wrong. You have not given your filename, just a directory.

From the codex

wp_enqueue_style( string $handle, string $src = false, array $deps = array(), string|bool|null $ver = false, string $media = 'all' )

so in your case, you should enqueue like so

add_action( 'wp_enqueue_scripts', 'wpse_my_style' );
function wpse_my_style(){
  wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . 'path/to/your/css' );
}    

the rest of the parameters are optional

-1
function wpdocs_style() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );  //This is for getting style.css of current theme
wp_enqueue_style( 'style-name1', get_teplate_directory_uri() . '/css/custom.css', array('jquery') );   //This is for getting other css file from parent directory  
wp_enqueue_script( 'script-name1', get_teplate_directory_uri() . '/js/custom.js', array('jquery') );   //This is for getting JS file from parent directory  
}
add_action( 'wp_enqueue_scripts', 'wpdocs_style' );
             OR
//Or as an laternate you can hook with wp_head also
add_action( 'wp_head', 'wpdocs_style' );

In order to run the above function need to add <?php wp_enqueue_script("jquery"); ?> (If you use this action add_action( 'wp_enqueue_scripts', 'wpdocs_style' );) in header.php befor </head>.

You have to assign name to every style (like I assign style-name, style-name1 etc) and name should be unique. And if you want to qnqueue from child theme's functions.php then you have to use get_stylesheet_directory_uri() instead of get_teplate_directory_uri().

9
  • 2
    OP question does not mention jquery at all. And you should NOT just add it in to header.php you should enqueue it like the styles.
    – ngearing
    Commented Aug 19, 2016 at 5:46
  • get_stylesheet_directory_uri() will work for theme AND child theme
    – bynicolas
    Commented Aug 19, 2016 at 5:46
  • 2
    @Rishabh You hook jQuey exactly the same way you hook everything else. That is with wp_enqueue_scripts hook. You said that in order to run your code, we need to add jQuery in header.php before </head> why are you saying that? it's just not true. Especially not true when adding CSS files. So if you want to provide more information to help him and others that will read this answer, please do so in a way that is not confusing people. In no way CSS requires jQuery in order to run. With wp_enqueue_script you have a parameter that tells WP to load in <head> or before closing </body>
    – bynicolas
    Commented Aug 19, 2016 at 12:38
  • 2
    So instead of adding script all over the place making it more difficult to debug and read your code, you can add all scripts in the same place and use the $deps parameter which tells WP in which sequence to load the scripts/CSS in order to respect your dependencies.
    – bynicolas
    Commented Aug 19, 2016 at 12:40
  • 2
    Even though it works, I wouldn't recommend that as a standard practice, it's better to group all your script/CSS together, makes it easier for your (the dev) and other to read and understand your code.
    – bynicolas
    Commented Aug 19, 2016 at 14:27

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.