0

I have properly set up a child theme and am adding stylesheets and scripts in my new functions.php file. The only problem is, my newly enqueued stylesheets are being referenced using the parent theme when the files are located in the child theme folder.

Setting up child theme:

<?php
    add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
    function enqueue_child_theme_styles() {
      wp_enqueue_style( 'parent-theme', get_template_directory_uri().'/style.css' );
      wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array('parent-theme')  );
    }
?>

Adding new styles further into my child's functions.php file:

<?php
function new_scripts() {
    wp_enqueue_style( 'grid', get_template_directory_uri() . '/css/grid.css');
}
add_action( 'wp_enqueue_scripts', 'new_scripts' );

Which shows up this way in my when rendered:

<link media="all" type="text/css" href="/wp-content/themes/parent-theme/css/grid.css?ver=4.2.2" id="grid-css" rel="stylesheet">

I need it to reference href="/wp-content/themes/child-theme/css/grid.css?ver=4.2.2"

Any suggestions?

3
  • Use wp_enqueue_style( 'grid', get_stylesheet_directory_uri() . '/css/grid.css'); May 13, 2015 at 5:10
  • The method you are using to enqueue the parent style is wrong. Wish that the person who constantly changes the codex would get a cramp :-). This is the correct method. In your new_script() function, get_template_directory_uri() should be get_stylesheet_directory_uri() May 13, 2015 at 5:15
  • Thanks for the update on the codex. When I was first learning to use a child theme they had just stopped using @import and I couldn't find a consensus anywhere on the correct methodology.
    – Heather
    May 13, 2015 at 5:50

2 Answers 2

1

You can use get_stylesheet_directory_uri(); while enqueuing the scripts and styles. It returns the directory path in which your child stylesheet is stored.

1

Just a quick note, if you include the PHP_INT_MAX variable in your add_action call, the child theme will not work correctly!

Do NOT do this ...

add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);

You should rather do this ...

add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles');

Then it will import the child theme correctly.

2
  • Can you explain, in the question, why PHP_INT_MAX will not work?
    – kero
    Dec 10, 2019 at 15:06
  • I don't know ... I found the answer somewhere else, so I thought that I would share it here.
    – AndrewG
    Apr 12, 2021 at 7:41

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.