has_blocks

函数


has_blocks ( $post = null )
参数
  • (int|string|WP_Post|null)
    $post
    Optional. Post content, post ID, or post object. Defaults to global $post.
    Required:
    Default: null
返回值
  • (bool) Whether the post has blocks.
相关
  • parse_blocks()
定义位置
  • wp-includes/blocks.php
    , line 567
引入
5.0.0
弃用

判断一个文章或内容字符串是否有区块。

这个测试优化了性能,而不是严格的准确性,检测区块的模式,但不验证其结构。对于严格的准确性,你应该在文章内容上使用区块分析器。

function has_blocks( $post = null ) {
	if ( ! is_string( $post ) ) {
		$wp_post = get_post( $post );

		if ( ! $wp_post instanceof WP_Post ) {
			return false;
		}

		$post = $wp_post->post_content;
	}

	return false !== strpos( (string) $post, '