Home › Forums › Gutenberg › Register block preview image with acf_register_block_type? › Reply To: Register block preview image with acf_register_block_type?
Also managed to solve this reading trough this topic, thank you all! My solution may work for others so also posting it here:
In my “block.json”:
{
"name": "acf/my-acf-block",
"title": "My ACF Block",
"description": "Description of this block.",
"style": "file:./my-acf-block.css",
"category": "my-custom-category",
"icon": "random-icon-or-svg",
"textdomain": "my-textdomain",
"acf": {
"mode": "edit",
"renderTemplate": "my-acf-block.php"
},
"supports": {
"align": [
"full"
]
},
"example": {
"attributes": {
"mode": "preview",
"data": {
"preview_image_my_acf_block": "assets/img/my-placeholder.jpg"
}
}
}
}
And in “my-acf-block.php” I start with:
<?php
if( isset( $block['data']['preview_image_my_acf_block'] ) ) {
echo '<img src="' . CONSTANT_THEME_URL . $block['data']['preview_image_my_acf_block'] . ' " style="width: 100%; height: auto;">';
return;
}
// the rest of my template render..
?>
The “CONSTANT_THEME_URL” is a constant that I normally define within my theme and returns the URL of the current theme, but including trailingslashit().
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.