获取没有 the_content() 的古腾堡块

Get Gutenberg Blocks without the_content()

我正在尝试显示来自另一个特定 post ID 的古腾堡块。

问题是,是否存在一种功能,我可以从一个 post 中获取所有块并将其显示在站点的任何位置?就像 get_the_content 一样?

我想你可以通过这种方式获得古腾堡积木。

$post_id = 1;
$post = get_post( $post_id ); 

if ( has_blocks( $post->post_content ) ) {
    $blocks = parse_blocks( $post->post_content );
    print'<pre>';print_r($blocks);print'</pre>';
    foreach( $blocks as $block ) {
        echo render_block( $block );
    }
}

注意:代码我没有自己测试过

$post_id = 1; // ID of the post
// parse_blocks parses blocks out of
// a content string into an array
$blocks = parse_blocks( get_the_content( $post_id ) );
$content_markup = '';
foreach ( $blocks as $block ) {
    // render_block renders a single block into a HTML string
    $content_markup .= render_block( $block );
}
// this will apply the_content filters for shortcodes
// and embeds to contiune working
echo apply_filters( 'the_content', $content_markup );