【问题标题】:How to list and re-arrange or manipulate Gutenberg Block Categories in Wordpress如何在 Wordpress 中列出和重新排列或操作 Gutenberg 块类别
【发布时间】:2019-06-08 15:55:26
【问题描述】:

任何人都知道如何在 Wordpress 的 Gutenberg 编辑器中重新排列、操作块类别我什至无法像使用块本身那样返回它们的列表,我能找到的只是“getCategories”,它没有似乎没有做任何事情......新文档一点也不好。

【问题讨论】:

标签: javascript php wordpress wordpress-gutenberg


【解决方案1】:

这为我注册了一个自定义 Guttenberg 块并使其成为 WP Admin 编辑器中的第一个选项起到了作用:

function custom_block_category( $categories ) {
    $custom_block = array(
        'slug'  => 'my-blocks',
        'title' => __( 'My Test Blocks', 'my-blocks' ),
    );

    $categories_sorted = array();
    $categories_sorted[0] = $custom_block;

    foreach ($categories as $category) {
        $categories_sorted[] = $category;
    }

    return $categories_sorted;
}
add_filter( 'block_categories', 'custom_block_category', 10, 2);

【讨论】:

    【解决方案2】:

    这是一个更简短的解决方案:

    function custom_block_category( $categories ) {
        return array_merge(
            array(
                array(
                    'slug' => 'my-blocks',
                    'title' => __( 'My Test Blocks', 'my-blocks' ),
                ),
            ),
            $categories
        );
    }
    add_filter( 'block_categories', 'custom_block_category', 10, 2 );
    

    【讨论】:

    • 这将从列表中删除任何其他类别。
    • 你确定吗?它看起来很简单,基本上应该将新类别附加到现有类别中。我也刚查了。之前是6个类别,之后是7个类别。此外,它与 WordPress 开发手册 (developer.wordpress.org/block-editor/developers/filters/…) 中列出的方法基本相同,只是更改了顺序,从而在前面插入了自己的类别。
    猜你喜欢
    • 2020-02-19
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多