如何更改WP Gutenberg的类别组件面板?

时间:2019-06-14 06:46:27

标签: wordpress wordpress-gutenberg

在Gutenberg编辑器中,我尝试修改类别面板(在右侧面板中选择要放置帖子的类别)。 如果该类别包含子类别,则应该不能在该类别中添加帖子。由于类别是静态的,因此可以使用category-id。

我的想法是使用enqueue_block_editor_assets并添加一些JavaScript以通过元素ID禁用复选框。 这不起作用,找不到元素:-(

这是我到目前为止无法使用的代码:

functions.php:

function gutenberg_enqueue()
{
    wp_enqueue_script(
        'gutenberg-additions-script',
        get_stylesheet_directory_uri().'/gutenberg-additions.js',
        array(), true, true
    );
}
add_action('enqueue_block_editor_assets', 'gutenberg_enqueue', 999);

(我使用get_stylesheet_directory_uri(),因为我处于儿童主题)

gutenberg-additions.js:

window.onload = function () {
    var cat1 = document.getElementById('editor-post-taxonomies-hierarchical-term-1');
    if (cat1 != null) {
        cat1.disabled = true;
    }

1 个答案:

答案 0 :(得分:0)

欢迎使用Stackoverflow。 gutenberg github directory中有一个有关此示例。它是使用旧的es5语法编写的,但应该可以轻松转移到esnext。它使用editor.PostTaxonomyType过滤器来编辑分类法组件。

var el = wp.element.createElement;

function customizeProductTypeSelector( OriginalComponent ) {
    return function( props ) {
        if ( props.slug === 'product-type' ) {
            return el(
                'div',
                {},
                'Product Type Selector'
            );
        } else {
            return el(
                OriginalComponent,
                props
            );
        }
    }
};

wp.hooks.addFilter(
    'editor.PostTaxonomyType',
    'my-custom-plugin',
    customizeProductTypeSelector
);

如果您需要更多信息,还请阅读this github issue上的评论。