Poet 提供简单的基于配置的帖子类型、分类、编辑器调色板、块类别、块模式和块注册/修改。
Tools
子菜单。 通过 Composer 安装:
$ composer require log1x/poet
首先使用 Acorn 发布 Poet 配置文件:
$ wp acorn vendor:publish --provider= " Log1xPoetPoetServiceProvider "
所有与 Poet 相关的配置都位于config/poet.php
中。在这里,您将找到一个预先配置了一些常见设置的示例图书帖子类型:
' post ' => [
' book ' => [
' enter_title_here ' => ' Enter book title ' ,
' menu_icon ' => ' dashicons-book-alt ' ,
' supports ' => [ ' title ' , ' editor ' , ' author ' , ' revisions ' , ' thumbnail ' ],
' show_in_rest ' => true ,
' has_archive ' => false ,
' labels ' => [
' singular ' => ' Book ' ,
' plural ' => ' Books ' ,
],
],
],
在最简单的形式中,可以通过简单地传递字符串来创建帖子类型:
' post ' => [
' book ' ,
],
要修改现有的帖子类型,只需将其视为正在创建新的帖子类型,仅传递您希望更改的配置选项:
' post ' => [
' post ' => [
' labels ' => [
' singular ' => ' Article ' ,
' plural ' => ' Articles ' ,
],
],
],
还可以通过简单地传递false
来取消注册现有的帖子类型:
' post ' => [
' book ' => false ,
],
请注意,某些内置帖子类型(例如帖子)无法按常规取消注册。
有关帖子类型的其他配置选项,请参阅:
register_post_type()
register_extended_post_type()
注意:不要将配置嵌套在
config
键中,如扩展 CPT 文档中所示。
注册分类法类似于帖子类型。查看config/poet.php
,您将看到默认图书帖子类型附带的流派分类法:
' taxonomy ' => [
' genre ' => [
' links ' => [ ' book ' ],
' meta_box ' => ' radio ' ,
],
],
最相关的配置选项是定义分类法连接到的帖子类型的links
。如果没有指定链接,则默认为post
。
要查看流派分类的存档,请将名为archive.blade.php
的 Blade 模板复制到名为taxonomy-genre.blade.php
的新文件中。
在最简单的形式中,您可以简单地传递一个字符串。下面的示例将为帖子帖子类型创建主题分类:
' taxonomy ' => [
' topic ' ,
],
与帖子类型一样,要修改现有分类法,只需传递您想要更改的配置选项即可:
' taxonomy ' => [
' category ' => [
' labels ' => [
' singular ' => ' Section ' ,
' plural ' => ' Sections ' ,
],
],
],
与帖子类型一样,您只需传递false
即可轻松取消注册现有分类法:
' taxonomy ' => [
' post_tag ' => false ,
' category ' => false ,
],
有关分类法的其他配置选项,请参阅:
register_taxonomy()
register_extended_taxonomy()
注意:不要将配置嵌套在
config
键中,如扩展 CPT 文档中所示。
Poet 提供了一种简单的方法来使用编辑器注册古腾堡块,使用随附的 Blade 视图在前端渲染该块。
使用在编辑器中注册块时定义的namespace/label
来注册块。
如果未提供命名空间,则将使用当前主题的文本域。
在大多数情况下注册块非常简单:
' block ' => [
' sage/accordion ' ,
],
给定块sage/accordion
,您随附的 Blade 视图将位于views/blocks/accordion.blade.php
。
块视图具有以下可用变量:
$data
– 包含块数据的对象。$content
– 包含 InnerBlocks 内容的字符串。为空时返回null
。默认情况下,当检查$content
是否为空时,它会通过一个方法传递,以在评估之前删除所有标签和空格。这保证了编辑器像nbsp;
或空的<p></p>
标签不会导致$content
在条件中使用时始终返回true
。
如果您不希望在特定块上出现此行为,只需将其注册为数组即可:
' block ' => [
' sage/accordion ' => [ ' strip ' => false ],
],
如果您需要在特定块上使用 PHP 注册块属性,只需在注册时将属性传递到数组中即可:
' block ' => [
' sage/accordion ' => [
' attributes ' => [
' title ' => [
' default ' => ' Lorem ipsum ' ,
' type ' => ' string ' ,
],
],
],
],
考虑一个使用title
和className
属性注册的折叠块。您的视图可能如下所示:
<div class="wp-block-accordion {{ $ data -> className ?? '' }}">
@ isset ( $ data -> title )
<h2>{!! $ data -> title !!}</h2>
@endisset
<div>
{!! $ content ?? ' Please feed me InnerBlocks. ' !!}
</div>
</div>
Poet 提供了一种简单的方式来注册、修改和取消注册古腾堡块类别。查看配置,您将看到“号召性用语”类别的注释掉示例:
' block_category ' => [
' cta ' => [
' title ' => ' Call to Action ' ,
' icon ' => ' star-filled ' ,
],
],
这将产生一个带有cta
块的块类别。注册块类别后,您必须先将块注册到其 slug,然后该类别才会出现在编辑器中。
在最简单的形式中,您可以简单地传递一个字符串:
' block_category ' => [
' my-cool-blocks ' ,
],
这将导致my-cool-blocks
类别自动将 slug 转换为标题大小写。
您还可以通过将值传递给 slug 来指定标题:
' block_category ' => [
' my-cool-blocks ' => ' Best Blocks, World. ' ,
],
与帖子类型和分类法一样,修改现有的块类别与注册块类别相同:
' block_category ' => [
' layouts ' => ' Sections ' ,
' common ' => [ ' icon ' => ' star-filled ' ],
],
您可以通过简单地传递false
来取消注册现有的块类别:
' block_category ' => [
' common ' => false ,
],
Poet 还可以为您注册块模式,并提供可选的内容刀片视图。
使用在编辑器中注册模式时定义的namespace/label
来注册模式。
如果未提供命名空间,则将使用当前主题的文本域。
在大多数情况下注册块非常简单:
' block_pattern ' => [
' sage/hero ' => [
' title ' => ' Page Hero ' ,
' description ' => ' Draw attention to the main focus of the page, and highlight key CTAs ' ,
' categories ' => [ ' all ' ],
],
],
您也可以使用content
密钥在此处注册模式的实际内容。或者将其留空以使用相应的刀片视图。
' block_pattern ' => [
' sage/fake-paragraph ' => [
' title ' => ' Fake Paragraph ' ,
' description ' => ' Filler content used instead of actual content for testing purposes ' ,
' categories ' => [ ' all ' ],
' content ' => ' <!-- wp:paragraph --><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione nulla culpa repudiandae nisi nostrum et, labore earum repellendus porro, mollitia voluptas quam? Modi sint tempore deleniti nesciunt ab, perferendis et.</p><!-- /wp:paragraph --> ' ,
],
],
给定块sage/fake-paragraph
,如果未定义content
键,则随附的 Blade 视图将位于views/block-patterns/fake-paragraph.blade.php
。
该块模式视图可能如下所示:
<!-- wp:paragraph -->
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione nulla culpa repudiandae nisi nostrum et, labore earum repellendus porro, mollitia voluptas quam? Modi sint tempore deleniti nesciunt ab, perferendis et.</p>
<!-- /wp:paragraph -->
可以在诗人配置中使用以下代码添加块模式类别:
' block_pattern_category ' => [
' all ' => [
' label ' => ' All Patterns ' ,
],
],
您可以根据块编辑器手册指定所有类别属性,例如label
。
注意:目前,如果根本没有可用的块模式类别,则编辑器中的“块模式”选项卡在单击时将会崩溃。
Poet 试图通过不需要如此严格、脆弱的数组标记来稍微简化在编辑器中注册调色板的过程。
虽然您当然可以直接传递所述数组,但您也可以通过简单地传递 slug 和颜色并让 Poet 处理其余部分来注册颜色。
' palette ' => [
' red ' => ' #ff0000 ' ,
' blue ' => ' #0000ff ' ,
],
除了传递数组之外,Poet 还接受包含调色板的JSON
文件。默认情况下,Poet 通常会在dist/
中查找该文件。
' palette ' => ' colors.json ' ,
如果您使用 Palette Webpack 插件,您也可以简单地传递true
来在构建过程中自动使用生成的palette.json
。
' palette ' => true ,
如果您发现 Poet 中的错误,请提出问题。
无论是通过 PR、报告问题还是提出想法,我们都鼓励并赞赏您做出贡献。
Poet 是根据 MIT 许可证提供的。