可以使用内置has_block( $block_type, $post ) 用于检查特定块的函数。
第一个输入参数是中的块类型<!-- wp:{$block_type} --> 第二个是post对象。
要查找相应的块类型,我们可以通过快捷方式在代码编辑器视图中查看它:
Ctrl键+Shift键+Alt键
或通过设置:

例如,库块将显示为:

Example: Within The Loop
正在检查
wp:gallery 和
wp:video 块,在循环中,相应的块类型为
gallery 和
video:
if ( has_block( \'video\' ) && has_block( \'gallery\' ) ) {
// ...
}
Example: Outside The Loop
对于给定的post对象,我们可以使用
has_block() 确定帖子是否包含
wp:gallery 和
wp:video 块:
$mypost = get_post( 123 );
if ( has_block( \'video\', $mypost ) && has_block( \'gallery\', $mypost ) ) {
// ...
}
Example: Custom Block Within The Loop
正在检查命名空间为的自定义块的内容:
<!-- wp:custom/block -->, 这将是:
if ( has_block( \'custom/block\' ) ) {
// ...
}