子主题.css文件不会覆盖其父.css文件

时间:2018-05-17 作者:harrison

上个月,我创建了我的第一个网站。在此之前,我没有编码知识,所以我学到的每一件事对我来说都是新的。

上周我的问题是我的孩子主题。css文件(和php.file,但这是另一回事)并没有覆盖它们的父文件。css文件。我整个星期都在努力寻找解决方案,但到目前为止没有成功。

这是我在子函数中输入的代码。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) -THIS FOLLOWING CODE WORKS, CAN ANYONE SEE POTENTIAL ISSUES WITH THIS CODE?

<?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 个回复
最合适的回答,由SO网友:harrison 整理而成

(非常感谢@D.Dan让我走上了正确的方向!)

这对我来说非常有效。即使在父主题更新我的孩子之后。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 );
?>

SO网友:D. Dan

您应该将以下行添加到my\\u theme\\u enqueue\\u styles函数中:

wp_dequeue_style( \'adforest-theme-modern\' );
wp_deregister_style( \'adforest-theme-modern\' );
因为他们是以这个名字添加的。您已经将父css排队,这样它就可以在您的css之前加载。

Edit: 尝试:

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 );

SO网友:dj.cowan

尝试以增量优先级运行两个不同的函数。。

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\') ) { ... }

结束

相关推荐

Debug errors on sidebar

我正在wordpress 3.8上使用一个儿童主题“Twenty14”。在调试模式下,我会遇到以下关于小部件的错误。注意:正在尝试获取\\wordpress\\wp content\\themes\\tema2014\\functions\\widgets中非对象的属性。php在线148update_post_meta($post->ID, \"_sidebar\", $_POST[\"link\"]); 以及注意:未定义的索引:link in\\wordpress\\wp content