我正在尝试显示另一个帖子中特定帖子ID的gutenberg块。
问题是,是否存在可以从一篇文章中获取所有块并将其显示在网站上任何位置的功能?就像get_the_content一样吗?
答案 0 :(得分:1)
$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 );
答案 1 :(得分:0)
我认为您可以通过这种方式获得古腾堡的地块。
$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 );
}
}
注意:我还没有亲自测试过代码。