英文:
Template for CPT (Block Themes)
问题
我想为我的CPT(例如,Book)设置特定的区块结构(例如,图像、标题、段落)。当用户创建一个新的Book类型的文章时,它必须在文章中看到这个结构,并具有编辑和发布的能力。
我如何看待解决方案:
- 在
register_post_type()
中添加模板:
function myplugin_register_book_post_type() {
$args = array(
'public' => true,
'label' => 'Books',
'show_in_rest' => true,
'template' => array(
array( 'core/image', array(
'align' => 'left',
) ),
array( 'core/heading', array(
'placeholder' => 'Add Author...',
) ),
array( 'core/paragraph', array(
'placeholder' => 'Add Description...',
) ),
),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'myplugin_register_book_post_type' );
- 在模板文件夹中添加
single-book.html
模板,具有以下结构:
<!-- wp:image {"align":"left"} /-->
<!-- wp:heading {"placeholder":"Add Author..."} /-->
<!-- wp:paragraph {"placeholder":"Add Description..."} /-->
但是,当我发布一篇文章时,在前端看不到任何内容。我尝试通过使用可重复使用的区块来解决这个问题,但是我的结构变成了类似于 <!-- wp:block {"ref":"51"} /-->
的形式。
这种方法有效,但不是我所需要的方式 - 每次保存新文章时,也会重新编写我的模板和可重复使用的区块(https://github.com/WordPress/gutenberg/issues/29269)。
我更喜欢我的第一个解决方案,但我是否遗漏了什么?也许我需要在模板中添加一些属性,但在文档中找不到相关信息。
英文:
I want to set for my CPT (ex. Book) a specific structure of blocks (ex. image, heading, paragraph). When the user creates a new post type Book it must see this structure in a post and have the ability to edit and publish it.
How I see the solution:
- Add template in register_post_type();
function myplugin_register_book_post_type() {
$args = array(
'public' => true,
'label' => 'Books',
'show_in_rest' => true,
'template' => array(
array( 'core/image', array(
'align' => 'left',
) ),
array( 'core/heading', array(
'placeholder' => 'Add Author...',
) ),
array( 'core/paragraph', array(
'placeholder' => 'Add Description...',
) ),
),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'myplugin_register_book_post_type' );
- Add single-book.html template in the templates folder with this structure:
<!-- wp:image {"align":"left"} /-->
<!-- wp:heading {"placeholder":"Add Author..."} /-->
<!-- wp:paragraph {"placeholder":"Add Description..."} /-->
But when I publish a post I don't see anything on frontend. I tried to solve this by using reusable blocks and instead, my structure was something like <!-- wp:block {"ref":"51"} /-->
It works, but not the way as I need - every time, when I save my new post changes also rewrite my template and reusable block (https://github.com/WordPress/gutenberg/issues/29269)
I prefer my first solution, but what am I missing? Maybe I need to add somehow attributes to my template, can't find info in documentation.
答案1
得分: 0
我找到了问题 - 我不需要在模板 single-book.html 中添加与我在 register_post_type() 中使用的相同的块结构。
我只需要在 single-book.html 中添加块 <!-- wp:post-content /-->
。
现在一切都正常。
英文:
I found the issue - I don't need to add in the template single-book.html the same block structure, as I use in register_post_type();
I need just to add in single-book.html block <!-- wp:post-content /-->
Now all works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论