Файлы дочерней темы.css не переопределяют родительские файлы.css
В прошлом месяце я создавал свой самый первый веб-сайт. До этого у меня не было знаний в области программирования, поэтому все, что я узнал, было для меня новым.
Моя проблема за последнюю неделю заключается в том, что мои дочерние файлы theme.css (и php.файл, но это другое дело) не переопределяют свои родительские файлы.css. Я пытался найти решение всю неделю, но пока безуспешно.
Это код, который я вложил в ребенка functions.php файл
=============================================================== (5) - Этот код позволяет читать дочернюю тему последней, но некоторые другие css (которые должны полагаться на что-то другое?) вызывают некоторые ошибки/
<?php
function my_theme_enqueue_styles() {
$parent_style = 'adforest-original-modern'; // This is 'adforest-style' for the AdForest theme.
wp_enqueue_style( $parent_style, get_template_directory_uri().'/css/modern.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri().'/modern.css',
array( $parent_style ),
wp_get_theme()->get('1.0.0'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function wp_67472455() {
wp_dequeue_style( 'adforest-theme-modern' );
wp_deregister_style( 'adforest-theme-modern' );
}
add_action( 'wp_print_styles', 'wp_67472455', 100 );
?>
==========================================================
(6) - ЭТОТ СЛЕДУЮЩИЙ КОД РАБОТАЕТ, МОЖЕТ ЛИ КТО-НИБУДЬ УВИДЕТЬ ПОТЕНЦИАЛЬНЫЕ ПРОБЛЕМЫ С ЭТИМ КОДОМ?
<?php
function my_theme_enqueue_styles() {
/* =================== modern.css ==========================*/
$parent_style = 'adforest-original-modern'; // This is 'adforest-style' for the AdForest theme.
wp_enqueue_style( $parent_style, get_template_directory_uri().'/css/modern.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri().'/modern.css',
array( $parent_style ),
wp_get_theme()->get('1.0.0'));
/* =================== default.css ==========================*/
wp_enqueue_style( 'defualt-original-color', get_template_directory_uri().'/css/colors/defualt.css' );
wp_enqueue_style( 'child-defualt-color',
get_stylesheet_directory_uri().'/defualt.css',
array( 'defualt-original-color' ),
wp_get_theme()->get('1.0.0'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
function wp_67472455() {
/* =================== modern.css ==========================*/
wp_dequeue_style( 'adforest-theme-modern' );
wp_deregister_style( 'adforest-theme-modern' );
/* =================== default.css ==========================*/
wp_dequeue_style( 'defualt-color' );
wp_deregister_style( 'defualt-color' );
}
add_action( 'wp_print_styles', 'wp_67472455', 1 );
?>
3 answers
Попробуйте запустить две отдельные функции с добавочным приоритетом..
function my_enqueue_style_one() {
wp_register_style( 'style1', get_template_directory_uri() . '/style1.css' );
wp_enqueue_style( 'style1' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_style_one', 10 );
function my_enqueue_style_two() {
wp_register_style( 'style2', get_stylesheet_directory_uri() . '/style2.css' );
wp_enqueue_style( 'style2' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_style_two', 20 );
Также попробуйте зарегистрировать таблицы стилей перед тем, как ставить их в очередь для дополнительного контроля... например, условно проверить, не стоят ли они уже в очереди.
if ( ! wp_style_is( 'style1', 'enqueued') ) { ... }
Вы должны добавить эти строки в функцию my_theme_enqueue_styles:
wp_dequeue_style( 'adforest-theme-modern' );
wp_deregister_style( 'adforest-theme-modern' );
Так как именно под этим именем они были добавлены. И вы уже поставили в очередь родительский css таким образом, чтобы он загружался раньше вашего css.
Редактировать: Попробуйте:
function my_theme_enqueue_styles() {
$parent_style = 'adforest-theme-modern'; // This is 'adforest-style' for the AdForest theme.
wp_enqueue_style( $parent_style, get_template_directory_uri().'/css/modern.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri().'/modern.css',
array( $parent_style ),
wp_get_theme()->get('1.0.0'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function wp_67472455() {
wp_dequeue_style( 'adforest-theme-modern' );
wp_deregister_style( 'adforest-theme-modern' );
}
add_action( 'wp_print_styles', 'wp_67472455', 100 );
(Большое спасибо @D. Дэну за то, что направил меня в правильном направлении!)
Для меня это сработало идеально. Даже после обновления родительской темы мои дочерние файлы.css все еще были активны и переопределяли эквивалентные родительские файлы.css.
Этот код предназначен для нескольких css-файлов
<?php
function my_theme_enqueue_styles() {
/* =================== modern.css ==========================*/
$parent_style = 'adforest-original-modern'; // This is 'adforest-style' for the AdForest theme.
wp_enqueue_style( $parent_style, get_template_directory_uri().'/css/modern.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri().'/modern.css',
array( $parent_style ),
wp_get_theme()->get('1.0.0'));
/* =================== default.css ==========================*/
wp_enqueue_style( 'defualt-original-color', get_template_directory_uri().'/css/colors/defualt.css' );
wp_enqueue_style( 'child-defualt-color',
get_stylesheet_directory_uri().'/defualt.css',
array( 'defualt-original-color' ),
wp_get_theme()->get('1.0.0'));
/* =================== smart_wizard_theme_arrows.css ==========================*/
wp_enqueue_style( 'defualt_smart_wizard_theme_arrows', get_template_directory_uri().'/css/colors/smart_wizard_theme_arrows.css' );
wp_enqueue_style( 'child-defualt_smart_wizard_theme_arrows',
get_stylesheet_directory_uri().'/smart_wizard_theme_arrows.css',
array( 'defualt_smart_wizard_theme_arrows' ),
wp_get_theme()->get('1.0.0'));
/* =================== forest-menu.css ==========================*/
wp_enqueue_style( 'original-forest-menu', get_template_directory_uri().'/css/forest-menu.css' );
wp_enqueue_style( 'child-forest-menu',
get_stylesheet_directory_uri().'/forest-menu.css',
array( 'original-forest-menu' ),
wp_get_theme()->get('1.0.0'));
/* =================== responsive-media-modern.css ==========================*/
wp_enqueue_style( 'responsive-original-modern', get_template_directory_uri().'responsive-media-modern.css' );
wp_enqueue_style( 'child-responsive-modern',
get_stylesheet_directory_uri().'/responsive-media-modern.css',
array( 'responsive-original-modern' ),
wp_get_theme()->get('1.0.0'));
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
function wp_67472455() {
/* =================== modern.css ==========================*/
wp_dequeue_style( 'adforest-theme-modern' );
wp_deregister_style( 'adforest-theme-modern' );
/* =================== default.css ==========================*/
wp_dequeue_style( 'defualt-color' );
wp_deregister_style( 'defualt-color' );
/* =================== smart_wizard_theme_arrows.css ==========================*/
wp_dequeue_style( 'adforest-wizard-arrows' );
wp_deregister_style( 'adforest-wizard-arrows' );
/* =================== forest-menu.css ==========================*/
wp_dequeue_style( 'forest-menu' );
wp_deregister_style( 'forest-menu' );
/* =================== responsive-media-modern.css ==========================*/
wp_dequeue_style( 'responsive-media-modern' );
wp_deregister_style( 'responsive-media-modern' );
}
add_action( 'wp_print_styles', 'wp_67472455', 1 );
?>