Joy
(@joyously)
If it’s specific content, a widget area works well.
But if you really want to use a reusable block, you can call get_posts
with the appropriate arguments, or get_post
if you know which one you want.
https://developer.wordpress.org/reference/functions/get_posts/
Thanks for the lead, Joy.
I solved this with the following php:
$reuse_block = get_post( 123 );
$reuse_block_content = $reuse_block->post_content;
echo $reuse_block_content;
I obtained the post id (‘123’) for the reusable block from the “Manage All Reusable Blocks” screen.
Joy
(@joyously)
That seems like a strange thing to hard code into a theme. And it doesn’t run the the filters for the content, so it might not display correctly(no texturized quotes or server side rendering of blocks?) and plugins can’t add to it (not that they should, just saying…).
Ah thanks for pointing that out. The reusable block already exists and is used by my client on various pages as a call to action. I don’t need nor want plugins to add to it. The client updates it approx monthly. We decided we wanted to put the same block at the halfway point of each article to drive more conversions. So I would go with “creative solution” as opposed to strange. I made the following correction. Does that ease your concerns?
corrected:
$reuse_block_content = apply_filters( 'the_content', $reuse_block->post_content);
Thanks!
That’s the final code:
$reuse_block = get_post( 123 ); // Where 123 is the post id
$reuse_block_content = apply_filters( 'the_content', $reuse_block->post_content);
echo $reuse_block_content;
You have to create a page or post with the blocks you want and insert it on your single.php where you want.
In my case I have a page named: Single Header and another page called Single Footer
-
This reply was modified 5 years ago by
gtamborero.
Not necessarily.
You can call the actual block by it’s post id without the need for an additional page.
To get the post id for a reusable block, edit any post (or create a new post) and click the add block button (+). Navigate to reusable blocks and in that menu there is a link to “Manage All Reusable Blocks”. Or you can go directly to the reusable block listing at site.com/wp-admin/edit.php?post_type=wp_block
In the listing of reusable blocks, you can hover over edit for any block and you can see the post ID in the link address in your status bar. Then you can call the block only without a new page using the code we detailed earlier.
Thanks @michael-barker ! That’s awesome!
No problem. It’s nice to help someone else on here for a change!
MB