2

I tried to make a child theme to change some colors. I used this https://codex.wordpress.org/Child_Themes tutorial, but I still can't get this done. The problem is that I am not able to override some styles, because my stylesheet called "bootstrap.css" is not getting loaded. My functions.php file looks like this

<?php
function my_theme_enqueue_styles() {
    $parent_style = 'maxstore_theme_stylesheets'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/css/bootstrap.css' );
    wp_enqueue_style( 'child-style', get_template_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version'));
    wp_enqueue_style( 'child-style', get_template_directory_uri() . '/css/bootstrap.css', array( $parent_style ), wp_get_theme()->get('Version'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

These are the files that are getting loaded enter image description here

EDIT 1: I'm now using this code and it still isn't working

<?php
function my_theme_enqueue_styles() {
    $parent_style = 'maxstore_theme_stylesheets'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/css/bootstrap.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version'));
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/css/bootstrap.css', array( $parent_style ), wp_get_theme()->get('Version'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

EDIT 2: My bootstrap stylesheet is getting loaded, but for some reason my website is still using another version of it. As you can see it uses 3 times the same styles and the one that is really used is version 3.3.4. and I want to use the one with the blue color which is version 1.0.2 enter image description here Version 3.3.4 in the picture and I want to use the bootstrap version 1.0.2. enter image description here EDIT 3: Code from functions.php that I am using at the moment

<?php
function my_theme_enqueue_styles() {

    $parent_styles = array('maxstore_theme_style', 'maxstore_theme_bootstrap'); 
// This is 'twentyfifteen-style' for the Twenty Fifteen theme.

    wp_enqueue_style( $parent_styles[0], get_template_directory_uri() . 
'/style.css' );
    wp_enqueue_style( $parent_styles[1], get_template_directory_uri() . 
'/css/bootstrap.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . 
'/style.css', $parent_styles, wp_get_theme()->get('Version'));
    wp_enqueue_style( 'child-bootstrap', get_stylesheet_directory_uri() . 
'/css/bootstrap.css', $parent_styles, wp_get_theme()->get('Version'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>
4

Parent vs Child Themes

get_template_directory_uri always refers to the parent theme you can verify this as you always should when things don't load as expected by looking in the console in the browsers dev tools. These would show 404 errors for the CSS files, in the wrong folder.

Instead, use get_stylesheet_directory_uri, the stylesheet family of functions always refer to the current active theme

Duplicated Names

wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version'));
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/css/bootstrap.css', array( $parent_style ), wp_get_theme()->get('Version'));

You can't call 2 stylesheets the same thing and expect it to work, which is what you've done with all 4 of your stylesheets

Also notice that you've loaded bootstrap.css from both themes

  • Ah okay, I’ll try that! – RuuddR Dec 23 '17 at 10:21
  • I tried it, but it still isn't loading for some reason. I can't find any error in the console if I press inspect element. – RuuddR Dec 23 '17 at 20:06
  • @xX4m4zingXx see my edits – Tom J Nowell Dec 24 '17 at 21:54
  • Oh yeah, I didn't update my post with the code that I am using currently. look at my edits. I am also loading both style.css files, which isn't causing any problems. – RuuddR Dec 24 '17 at 22:04
  • The version in the URL is just a cachebuster to force the browser to reload the file, it has nothing to do with controlling which version of bootstrap is loaded whatsoever. A lot of themes use timestamps/large numbers instead – Tom J Nowell Dec 25 '17 at 21:31
2

Each enqueue request needs a unique handle (or name), which is the first argument in the wp_enqueue_style hook. Since your first two and last two commands are given the same handle, it's probably only loading two of the four.

Try this. I followed your model of declaring a variable for the Parent Style handle. I've replaced $parent_style with an array containing two unique handles. Note that this means the dependency statement no longer needs an array, since the variable we're using is already one, as reflected in my code.

<?php
function my_theme_enqueue_styles() {
    $parent_styles = array( 'maxstore_theme_style', 'maxstore_theme_bootstrap' );
    wp_enqueue_style( $parent_styles[0], get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( $parent_styles[1], get_template_directory_uri() . '/css/bootstrap.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', $parent_styles, wp_get_theme()->get('Version'));
    wp_enqueue_style( 'child-bootstrap', get_stylesheet_directory_uri() . '/css/bootstrap.css', $parent_styles, wp_get_theme()->get('Version'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>
  • Now my bootstrap stylesheet is getting loaded, but it's still using an old version of it in my website, check my updated question – RuuddR Dec 24 '17 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.