最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp enqueue script - wp_enqueue_style referencing parent theme

programmeradmin4浏览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?

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?

Share Improve this question asked May 13, 2015 at 5:06 HeatherHeather 4992 gold badges8 silver badges21 bronze badges 3
  • Use wp_enqueue_style( 'grid', get_stylesheet_directory_uri() . '/css/grid.css'); – Mayeenul Islam Commented 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() – Pieter Goosen Commented 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 Commented May 13, 2015 at 5:50
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论