Home › Forums › Gutenberg › Register block preview image with acf_register_block_type? › Reply To: Register block preview image with acf_register_block_type?
The easiest way I think to do this is to pass the example
array to the acf_register_block_type
function and include “an additional non-custom-field value” as mentioned in the “Adding block previews 🌄” section of the [function’s docs](https://www.advancedcustomfields.com/resources/acf_register_block_type/).
In the rendering template, include a conditional to check for the is_example
flag and render a screenshot instead of the HTML, if it’s set. The screenshot could be an image, GIF, or even video (probably). See my conclusion after the code examples.
// init.php
acf_register_block_type([
...
'example' => [
'attributes' => [
'mode' => 'preview',
'data' => ['is_example' => true],
]
]
...
]);
// block.php
if (get_field('is_example')) :
/* Render screenshot for example */
else :
/* Render HTML for block */
endif;
That’s how I *think* it should work, but I have an issue with get_field
not returning data from the example
array. Am I missing something? The docs are pretty clear on how to include data for the example render, but it’s not working at all for me. I’m also not seeing any of the data that I’m passing in the example
array show up in the $block['data']
array as mentioned in the docs 😕
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.