3

I need to include some external css files, i use this snippet in functions.php but neither of them works.. Why..?

    add_action( 'wp_enqueue_scripts', 'add_css' );

function add_css() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
    wp_enqueue_style( 'style', get_template_directory_uri().'/assets/css/bootstrap.css' );
}

I'm using Twenty-Twenty-One child theme This is my style.css

/*Theme Name: Twenty Twenty-One Child
    Theme URI: https://wordpress.org/themes/twentytwentyone/
    Template: twentytwentyone
    Author: the WordPress team
    Author URI: https://wordpress.org/
    */
h2{
    color:red!important;
} */

h2 is to see if it works.

2 Answers 2

2

If you're using a child theme, you want to use get_stylesheet_directory_uri() instead of get_template_directory_uri - Unless the stylesheets are part of the parent theme.

function add_css() {
    wp_enqueue_style( 'parent-style', get_stylesheet_directory_uri().'/style.css' );
    wp_enqueue_style( 'style', get_stylesheet_directory_uri().'/assets/css/bootstrap.css' );
}
Sign up to request clarification or add additional context in comments.

8 Comments

It dosnt work, i appreciate this, maybe I will use it in the future
Where are you putting the files, with respect to your directory structure? Also, is style.css the Child Style or the parent style? Please clarify.
In wp-content/themes/ i have: twentytwentyone (original theme) and my child theme - twentytwentyone-child. i create style css following the instruction of wordpress.org that you can see up. Then i focus on functions.php but it doesnt work. " is style.css the Child Style or the parent style?" ... Sincerely i dont know.
Is the bootstrap.css getting added?
No.. I really don't uderstand. I try also with a plug-in to automatize it but nothing...
|
0

Depending on how your parent theme loads it's styles your function will vary. You can check the Enqueue stylesheet section in the Theme handbook for more clarification. Below is their example if the parent theme loads its style using a function starting with get_stylesheet

Make sure you are using the correct parent handle, 'parent-style' is used for TwentyFifteen theme for example.

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    $parenthandle = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    $theme = wp_get_theme();
    wp_enqueue_style( $parenthandle, get_template_directory_uri() . '/style.css', 
        array(),  // if the parent theme code has a dependency, copy it to here
        $theme->parent()->get('Version')
    );
    wp_enqueue_style( 'child-style', get_stylesheet_uri(),
        array( $parenthandle ),
        $theme->get('Version') // this only works if you have Version in the style header
    );
}

You should also look at the wp_enqueue_style code reference as the handle for your bootstrap style should probably not be style as it needs to be unique e.g. bootstrap-style

1 Comment

I try it with $parenthandle = ' twenty-twenty-one-style ' but doesnt work. I add in the question my style.css maybe there is something wrong in..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.