CPT(块主题)的模板

huangapple go评论57阅读模式
英文:

Template for CPT (Block Themes)

问题

我想为我的CPT(例如,Book)设置特定的区块结构(例如,图像、标题、段落)。当用户创建一个新的Book类型的文章时,它必须在文章中看到这个结构,并具有编辑和发布的能力。

我如何看待解决方案:

  1. 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' );
  1. 在模板文件夹中添加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:

  1. Add template in register_post_type();
function myplugin_register_book_post_type() {
    $args = array(
        &#39;public&#39; =&gt; true,
        &#39;label&#39;  =&gt; &#39;Books&#39;,
        &#39;show_in_rest&#39; =&gt; true,
        &#39;template&#39; =&gt; array(
            array( &#39;core/image&#39;, array(
                &#39;align&#39; =&gt; &#39;left&#39;,
            ) ),
            array( &#39;core/heading&#39;, array(
                &#39;placeholder&#39; =&gt; &#39;Add Author...&#39;,
            ) ),
            array( &#39;core/paragraph&#39;, array(
                &#39;placeholder&#39; =&gt; &#39;Add Description...&#39;,
            ) ),
        ),
    );
    register_post_type( &#39;book&#39;, $args );
}
add_action( &#39;init&#39;, &#39;myplugin_register_book_post_type&#39; );
  1. Add single-book.html template in the templates folder with this structure:
&lt;!-- wp:image {&quot;align&quot;:&quot;left&quot;} /--&gt;
&lt;!-- wp:heading {&quot;placeholder&quot;:&quot;Add Author...&quot;} /--&gt;
&lt;!-- wp:paragraph {&quot;placeholder&quot;:&quot;Add Description...&quot;} /--&gt;

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 &lt;!-- wp:block {&quot;ref&quot;:&quot;51&quot;} /--&gt;

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 &lt;!-- wp:post-content /--&gt;

Now all works

huangapple
  • 本文由 发表于 2023年6月16日 03:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定