has_blocks()

最后更新于:2021-11-27 04:40:28

has_blocks( int|string|GC_Post|null$post=null)

Determine whether a post or content string has blocks.

参数

$post

(int|string|GC_Post|null) (Optional) Post content, post ID, or post object. Defaults to global $post.

Default value: null

响应

(bool) Whether the post has blocks.

源文件

文件: gc-includes/blocks.php

function has_blocks( $post = null ) {
	if ( ! is_string( $post ) ) {
		$gc_post = get_post( $post );
		if ( $gc_post instanceof GC_Post ) {
			$post = $gc_post->post_content;
		}
	}

	return false !== strpos( (string) $post, '<!-- gc:' );
}
class GCDocs_Custom_Block_Parse {

	function parse() {
		// Do something
  	}
}

function gcdocs_custom_block_parser() {
	return 'GCDocs_Custom_Block_Parse';
}

/**
 * In the implementation check if content has block
 */
if ( has_blocks( $post_content ) ) {
	add_filter( 'block_parser_class', 'gcdocs_custom_block_parser' );
	$response_content = parse_blocks( $content );
	remove_filter( 'block_parser_class', 'gcdocs_custom_block_parser' );
}