programing

Wordpress에서 Gutenberg 블록 카테고리를 나열, 정렬 또는 조작하는 방법

linuxpc 2023. 2. 25. 19:50
반응형

Wordpress에서 Gutenberg 블록 카테고리를 나열, 정렬 또는 조작하는 방법

Wordpress의 Gutenberg Editor에서 Block Categories를 다시 정렬하고 조작하는 방법을 아는 사람이 있다면 Blocks 자체에서 할 수 있는 것처럼 목록을 반환할 수도 없습니다. 제가 찾을 수 있는 것은 아무것도 하지 않는 'get Categories'뿐입니다.새로운 문서는 전혀 좋지 않습니다.

이것으로 커스텀 Guttenberg 블록을 등록하여 WP Admin Editor의 첫 번째 옵션으로 만들 수 있었습니다.

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

간단한 해결책은 다음과 같습니다.

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

위의 솔루션은 PHP에 적합합니다.그러나 만약 누군가가 자바스크립트를 사용하여 그것을 하고 싶다면, 좋은 방법도 있다.

import { getCategories, setCategories } from '@wordpress/blocks';

const customBlockCategory = {
   slug: 'my-custom-block-category',
   title: __('My Custom Block Category', 'text-domain'),
   icon: 'wordpress', // or an SVG icon component as Element
}

// Then Just append the custom block category.
setCategories([
    ...getCategories(), // This is all default registered categories.
    customBlockCategory,
]);

처음에 카테고리를 추가하려면 먼저 카테고리를 추가합니다.

setCategories([
    customBlockCategory,
    ...getCategories(),
]);

그러면 이 카테고리 블록이 먼저 표시됩니다.

희망, 이것이 많은 사람들에게 도움이 될 것이다.

언급URL : https://stackoverflow.com/questions/54185278/how-to-list-and-re-arrange-or-manipulate-gutenberg-block-categories-in-wordpress

반응형