Gutenberg: Display dynamic blocks outside of the post content in the theme

Created on 11 Jul 2019  路  2Comments  路  Source: WordPress/gutenberg

I am creating a dynamic block (view with PHP). I have problems: i can't Display specific Gutenberg blocks of a post outside of the post content in the theme.

I watched this tutorial. it works for non dynamic blocks, but not with dynamic blocks.
single_my-template.php

$blocks = parse_blocks( get_the_content() ); foreach ( $blocks as $block ) { if ( 'test/test' === $block['blockName'] ) { echo do_shortcode( $block['innerHTML'] ); break; } }
The dynamic block is not displayed.

<?php the_content(); ?>
With the code above, it is displayed, but I want to display the block at a specific place, separately from other blocks.

[Type] Question

Most helpful comment

@anUserFr do_shortcode is for rendering shortcodes, not blocks.

The article mentions:

The project where I had to create that behavior for uses the Soliloquy slider, which comes with a Gutenberg block (in the end it stores a shortcode in the database).

So this is a specific workaround for that slider block, which outputs a shortcode.

You'll want to follow the steps in the rest of the article 'Displaying the rest of the content' where they use render_block( $block ); to get the block markup.

All 2 comments

@anUserFr do_shortcode is for rendering shortcodes, not blocks.

The article mentions:

The project where I had to create that behavior for uses the Soliloquy slider, which comes with a Gutenberg block (in the end it stores a shortcode in the database).

So this is a specific workaround for that slider block, which outputs a shortcode.

You'll want to follow the steps in the rest of the article 'Displaying the rest of the content' where they use render_block( $block ); to get the block markup.

Thank you so much. It functioned.

Was this page helpful?
0 / 5 - 0 ratings