One can use the built-in has_block( $block_type, $post )
function to check for specific blocks.
The first input argument is the block type in <!-- wp:{$block_type} -->
and the second one is the post object.
To find the corresponding block type, we can view it in the Code Editor view via the shortcut:
Ctrl + Shift + Alt + M
or via the settings:
For example the gallery block will show as:
Example: Within The Loop
Checking for wp:gallery
and wp:video
blocks, within the loop, the corresponding block types are gallery
and video
:
if ( has_block( 'video' ) && has_block( 'gallery' ) ) {
// ...
}
Example: Outside The Loop
For a given post object, we can use the second input argument of has_block()
to determince if the post contains the wp:gallery
and wp:video
blocks:
$mypost = get_post( 123 );
if ( has_block( 'video', $mypost ) && has_block( 'gallery', $mypost ) ) {
// ...
}
Example: Custom Block Within The Loop
Checking the content for a custom block with namespace: <!-- wp:custom/block -->
, it would be:
if ( has_block( 'custom/block' ) ) {
// ...
}