For the life of me, I can't find anything on how to do this: simply output a reusable gutenberg block via php in a theme template. Seems like it should be doable. Anyone?
Possibly answering my own question. Please tell me if there's a better/easier way to do this.
<?php
// get reusable gutenberg block:
$gblock = get_post( 7418 );
echo apply_filters( 'the_content', $gblock->post_content );
?>
The first downside I can see to this is that it's inconvenient to have to hunt down the post ID of the block.
I just found this handy little snippet. It adds the Reusable blocks as an admin link. Once there you can easily determine the ID of the reusable block that you need. https://github.com/WordPress/gutenberg/issues/15549
add_menu_page( 'linked_url', 'Reusable Blocks', 'read', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22 );
}
As pointed out by gtamborero here, you can use get_page_by_title(), but you need to specify that this is a 'wp_block'. His example works for me (using WP 5.8.1):
get_page_by_title( 'Your Title', OBJECT, 'wp_block' );
I'm using it like this:
$myPost = get_page_by_title( 'Your Title', OBJECT, 'wp_block' );
$myContent = apply_filters('the_content', $myPost->post_content);
echo $myContent;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With