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()
定義位置
相關方法
has_blockparse_blocksdo_blocks_flatten_blocksserialize_blocks
引入
5.0.0
棄用
-

has_blocks: 這是一個WordPress的函式,用來檢查一個文章或頁面的內容中是否存在任何塊。如果有任何塊存在,該函式返回一個布林值為true,否則為false。

判斷一個文章或內容字串是否有區塊。

這個測試優化了效能,而不是嚴格的準確性,檢測區塊的模式,但不驗證其結構。對於嚴格的準確性,你應該在文章內容上使用區塊分析器。

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, '<!-- wp:' );
}

常見問題

FAQs
檢視更多 >