$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Child theme enqueue scripts using new functions|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Child theme enqueue scripts using new functions

matteradmin9PV0评论

To enqueue I have used get_theme_file_uri('/style.css'). Now I have created a child theme using a generator and it created a code like the following; but it throws a php error with parent-style.

  1. Is the code below really needed if I am using get_theme_file_uri() in my parent theme as it will automatically search for file in child theme?
  2. I have some css code in parent theme (style.css). So if I use child theme (which is empty). So will the parent theme style get applied?

function theme_enqueue_styles() { wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css'); wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', array($parent_style)); } add_action('wp_enqueue_scripts', 'theme_enqueue_styles');

Thanks

To enqueue I have used get_theme_file_uri('/style.css'). Now I have created a child theme using a generator and it created a code like the following; but it throws a php error with parent-style.

  1. Is the code below really needed if I am using get_theme_file_uri() in my parent theme as it will automatically search for file in child theme?
  2. I have some css code in parent theme (style.css). So if I use child theme (which is empty). So will the parent theme style get applied?

function theme_enqueue_styles() { wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css'); wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', array($parent_style)); } add_action('wp_enqueue_scripts', 'theme_enqueue_styles');

Thanks

Share Improve this question edited Dec 22, 2018 at 17:52 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Dec 22, 2018 at 10:27 user145078user145078 14
  • yes parent theme style applied – vikrant zilpe Commented Dec 22, 2018 at 10:35
  • your new or custom css code add your child theme css file but active your child theme – vikrant zilpe Commented Dec 22, 2018 at 10:36
  • add this code child theme functions.php file – vikrant zilpe Commented Dec 22, 2018 at 10:38
  • // Add Parent Style add_action( 'wp_enqueue_scripts', 'eventry_child_scripts' ); function eventry_child_scripts() { wp_enqueue_style( 'parent-style', get_template_directory_uri(). '/style.css' ); } – vikrant zilpe Commented Dec 22, 2018 at 10:38
  • 1 get_theme_file_path() (for absolute file paths) and get_theme_file_uri() (for URLs) work just like get_template_part() in that they will automatically look in the child theme for that file first, then fallback to the parent theme. – vikrant zilpe Commented Dec 22, 2018 at 11:45
 |  Show 9 more comments

1 Answer 1

Reset to default 0

Is the code below really needed if i am using get_theme_file_uri() in my parent theme as it will automatically search for file in child theme?

No. If your parent theme uses get_theme_file_uri( '/style.css' ), then when you create a style.css file in your child theme, WordPress will load that instead. So it won't load the parent theme's styles at all, which I think is related to your next question.

I have some css code in parent theme (style.css). So if i use child theme(which is empty). so whether the parent theme style will get applied?

If your parent theme uses get_theme_file_uri( '/style.css' ), and your child theme has a style.css file, the parent the styles won't get loaded at all. get_theme_file() will load the first file it finds, not all of them.

If you want to load the parent theme's styles and the child theme's styles, you have two options:

  1. Update your parent theme to use get_parent_theme_file_uri( 'style.css' ). This will cause WordPress to always load style.css from the parent theme, even if a child theme is activated. If you want to load style.css from the child theme as well, then enqueue get_theme_file_uri( 'style.css' ) from the child theme. Or,
  2. Leave your parent theme the way it is, and then enqueue the parent theme's stylesheet from the child theme by enqueueing get_parent_theme_file_uri( 'style.css' ) from the child theme.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far