最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

themes - Is it possible to access Gutenbergs reusable blocks in CPT block templates?

programmeradmin0浏览0评论

What I am trying to do is to use specific Reusable Blocks, that are editable by certain admins in the back end, within block templates for custom post types. So if you register a CTP or edit the capabilities of standard post types, you can use them like:

    function create_add_post_types() {
    register_post_type( 'product',
    array(
      'public' => true,
      'capability_type' => 'page',
      'show_in_rest' => true,
      'template' => array(
            array( 'core/heading', array(
                   'placeholder' => 'Füge eine Überschrift hinzu',
                   'content' => 'Projektname - Stadt, Land',
                   'level' => 1,
            ) ),
        )
    );
    }
    add_action( 'init', 'create_add_post_types' );

..except that insteadt of a core block like the heading here a reusable block is called.

What I am trying to do is to use specific Reusable Blocks, that are editable by certain admins in the back end, within block templates for custom post types. So if you register a CTP or edit the capabilities of standard post types, you can use them like:

    function create_add_post_types() {
    register_post_type( 'product',
    array(
      'public' => true,
      'capability_type' => 'page',
      'show_in_rest' => true,
      'template' => array(
            array( 'core/heading', array(
                   'placeholder' => 'Füge eine Überschrift hinzu',
                   'content' => 'Projektname - Stadt, Land',
                   'level' => 1,
            ) ),
        )
    );
    }
    add_action( 'init', 'create_add_post_types' );

..except that insteadt of a core block like the heading here a reusable block is called.

Share Improve this question edited Feb 7, 2019 at 13:52 Playnary asked Feb 7, 2019 at 13:06 PlaynaryPlaynary 3253 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

I actually found a solution myself by following the way a reusable block is registered in this stackexchange thread:

Reusable blocks are all stored in/as core/block and this block has the attribute ref which carries the post id of that block.

So, you can use

array( 'core/block', array(
 'ref' => [your block ID],
) ),

and the ID of every registered block is visible via the URL of that block at [your-wordpress]/wp-admin/edit.php?post_type=wp_block

You can call a reusable block as you call to any post type:

$args = array(
    'post_type' => 'wp_block',
...

OR

get_page_by_title( 'Your Title', OBJECT, 'wp_block' );
发布评论

评论列表(0)

  1. 暂无评论