【问题标题】:Wordpress - how to override Divi's custom post type stylesheetWordpress - 如何覆盖 Divi 的自定义帖子类型样式表
【发布时间】:2022-01-16 03:24:23
【问题描述】:

在将 Wordpress 子主题样式表 the correct way 排入队列时,新样式会覆盖父样式。

但是,由于 Divi 引入了对自定义帖子类型的 Builder 支持,因此添加了新的样式表 style-cpt.css。

此样式表中的所有样式(不幸的是,其中很多样式后面都有!important)在入队的子样式之后声明,因此将覆盖所有匹配的样式。

有没有办法覆盖这种“自定义”样式表?

【问题讨论】:

    标签: css wordpress custom-post-type


    【解决方案1】:

    经过一些实验,我发现functions.php 中的以下代码可以工作...(请注意,这会将标准主题样式表以及Divi 的自定义post child 主题排入队列)。 您可以在子主题文件夹中的自己的 style-cpt.css 文件中包含所有要覆盖的样式。

    function my_theme_enqueue_styles() {
    
        $parent_style = 'divi-style';
        $template_directory = get_template_directory_uri();
        $stylesheet_directory = get_stylesheet_directory_uri();
    
        wp_enqueue_style( $parent_style, $template_directory . '/style.css' );
        wp_enqueue_style( 'child-style',
            $stylesheet_directory . '/style.css',
            array( $parent_style ),
            wp_get_theme()->get('Version')
        );
    
        $parent_style = 'divi-cpt-style'; 
    
        wp_enqueue_style( $parent_style, $template_directory . '/style-cpt.css' );
        wp_enqueue_style( 'child-cpt-style',
            $stylesheet_directory . '/style-cpt.css',
            array( $parent_style ),
            wp_get_theme()->get('Version')
        );
    }
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
    

    【讨论】:

    • 这种方法的唯一问题似乎是样式表被加载了两次。有人知道为什么吗?
    • 好的,最近版本的 Divi 已经没有这个问题了。
    【解决方案2】:

    我正在使用它并且工作正常:

    function disable_cptdivi(){
    remove_action( 'wp_enqueue_scripts', 'et_divi_replace_stylesheet', 99999998 ); } add_action('init', 'disable_cptdivi');
    

    【讨论】:

    • 感谢@geoplous,但这不会完全停止对 Divi 样式表进行排队,从而阻止 Divi 正常工作吗?
    • 我在一个没有问题的项目上做了这个解决方案。 Divi 和 Divi-child-theme style.css 都运行良好。 gist.github.com/felixhirschfeld/…
    【解决方案3】:

    “remove_action(...);”从 Divi 版本 4.10.6(21 年 9 月)开始,解决方案不再有效,因为“et_divi_replace_stylesheet”操作已被删除。

    我解决这个问题的方法是从 Divi/includes/builder/core.php 覆盖第 776 行(从版本 4.14.6 开始),以便函数 et_builder_should_wrap_styles() 始终返回 false:

    function et_builder_should_wrap_styles() {
        static $should_wrap = null;
    
        if ( null === $should_wrap ) {
            $post_id = get_the_ID();
    
            // Warp on custom post type archives and on non-native custom post types when the builder is used.
            $should_wrap = et_builder_is_custom_post_type_archive() || ( et_builder_post_is_of_custom_post_type( $post_id ) && et_pb_is_pagebuilder_used( $post_id ) );
        }
    
        // return $should_wrap; /*** ORIGINAL CODE ***/
        return false;           /*** NEW CODE ***/
    }
    

    然后,为了确保在 Divi 更新时不会丢失此编辑,我设置了一个操作以在每次 Divi 更新时自动重写此行:

    add_action('upgrader_process_complete', 'edit_files_on_update'), 10, 2);
    
    function edit_files_on_update($upgrader_object, $hook_extra) {
        if ($hook_extra['action'] !== 'update') return false;
    
        if ($hook_extra['type'] === 'theme' && isset($hook_extra['themes'])) {
    
            // Divi - disable CPT CSS wrapper
            if (array_search('Divi', $hook_extra['themes']) !== false) {
                $file_location = get_template_directory().'/includes/builder/core.php';
                $file_contents = file_get_contents($file_location);
                $file_contents = str_replace('return $should_wrap;', 'return false;', $file_contents);
                file_put_contents($file_location, $file_contents);
            }
    
        }
    }
    

    我知道编辑主题的原始文件在技术上是不正确的,但“正确”的替代方法是覆盖我的子主题中的整个 Divi/includes/builder/core.php 文件,该文件长 7253 行!未来的 Divi 更新很有可能会编辑该原始文件,让我没有在子主题版本中反映这些编辑。

    【讨论】:

    • 哇!我希望有一个更简单的解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2014-01-03
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多