How to load WordPress css theme after parent css theme

In my WordPress theme file, css is loaded before the main css theme. My child theme css functions.php file is below

function my_theme_enqueue_styles(){ wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/../enfold-child/plugins/bootstrap/css/bootstrap.min.css' ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); 

I want to load a child css theme after the parent css theme.

+5
source share
2 answers

Add priority. Here 99 is high, so probably this will be the last, but some plugins can add css with a higher priority, although this is rare.

 function my_theme_enqueue_styles(){ wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/../enfold-child/plugins/bootstrap/css/bootstrap.min.css' ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 99 ); 

See: https://developer.wordpress.org/reference/functions/add_action/

+2
source

According to the documentation ( https://codex.wordpress.org/Child_Themes ), set the parent style as a dependency on the child style and load it into your functions.php:

 <?php function my_theme_enqueue_styles() { // You'll find this somewhere in the parent theme in a wp_enqueue_style('parent-style'). $parent_style = 'parent-style'; 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 ), wp_get_theme()->get('Version') ); } add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); ?> 

However, keep in mind that some themes use more than one style sheet. If this happens, you can add any of them to the function as follows:

  $parent_style = 'parent-style'; $parent_style2 = 'other-parent-style'; wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' ); wp_enqueue_style( $parent_style2, get_template_directory_uri() . '/inc/css/style.css' ); wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style, $parent_style2 ), wp_get_theme()->get('Version') ); 
0
source

Source: https://habr.com/ru/post/1263584/


All Articles