WordPress - 如何覆盖 Divi 的自定义帖子类型样式表

cla*_*ica 0 css wordpress custom-post-type

当以正确的方式将 WordPress 子主题样式表排入队列时,新样式将覆盖父样式。

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

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

有什么方法可以覆盖这种“自定义”样式表吗?

小智 6

“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 ***/
}
Run Code Online (Sandbox Code Playgroud)

然后,为了确保 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);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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