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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.