【发布时间】:2019-11-27 14:58:01
【问题描述】:
我正在尝试在另一个帖子中显示来自特定帖子 ID 的古腾堡块。
问题是,是否存在可以从一篇文章中获取所有块并将其显示在站点中的任何位置的功能?就像 get_the_content 一样?
【问题讨论】:
标签: php wordpress wordpress-gutenberg gutenberg-blocks
我正在尝试在另一个帖子中显示来自特定帖子 ID 的古腾堡块。
问题是,是否存在可以从一篇文章中获取所有块并将其显示在站点中的任何位置的功能?就像 get_the_content 一样?
【问题讨论】:
标签: php wordpress wordpress-gutenberg gutenberg-blocks
我认为您可以使用这种方式获得古腾堡的块。
$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 );
}
}
注意:我没有自己测试过代码。
【讨论】:
foreach 循环并在其中渲染块。您也可以从中删除print。
render_block。 developer.wordpress.org/reference/hooks/render_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 );
【讨论】: