Hiya
I have been following the code examples on:
https://github.com/WordPress/gutenberg/blob/master/docs/templates.md
To be able to add additional blocks to forma block template. Is there any way to integrate an ACF block into these templates?
My code so far is:
function my_add_template_to_posts() {
$post_type_object = get_post_type_object( 'page' );
$post_type_object->template = array(
array( 'core/paragraph', array(
'placeholder' => 'Add a root-level paragraph',
) ),
array( 'core/columns', array(), array(
array( 'core/column', array(), array(
array( 'core/image', array() ),
) ),
array( 'core/column', array(), array(
array( 'core/paragraph', array(
'placeholder' => 'Add a inner paragraph'
) ),
) ),
) )
);
$post_type_object->template_lock = 'all';
}
add_action( 'init', 'my_add_template_to_posts' );
But not sure how to reference a pre made ACF block?
Any help would be grateful
TIA
Tom
Hi Tom,
I was expirimenting with the same thing as i couldnt find information about this.
But for example:
First You create a custom block via acf_register_block:
acf_register_block(
array(
'name' => 'blockname',
'title' => __('title'),
'description' => __('description')
));
Then you can take the name of the acf_block ‘blockname’ and use it as following in your post type template:
$post_type_object->template = array(
array( 'acf/blockname', array() ),
);
Note: If your blockname contains a space or a underscore, acf renames this to a dash, so ‘block_name’ becomes ‘block-name’.
Hope this helps!
Taking this a bit further, is there a way to add default values in a template?
In the wordpress documentation I see that you can add for some core blocks a placeholder.
https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
Is there a way to add default content in acf blocks when you define them in the block template?
I second kkoyan’s question. Adding Gutenberg template blocks where the ACF block contents are preselected would be appreciated.
+1
I was expecting something like this to work, but computer says no …
array( ‘acf/blockname’, array(
‘header’ => __( ‘You may also like’, ‘td’ ),
) ),
where header is the name of the field.
+1
It would be very nice to predefine values for blocks in templates.
+1
It’d saves plenty of times, and give us the real power to use acf blocks as template
Hey Guys,
This is possible already.
Here is the post about it:
https://support.advancedcustomfields.com/forums/topic/programmatically-set-blocks-initial-value/
And the code which helps you to make better experiences! : )
The magic is that, you need to wrap them with a data []
'template' => [
['acf/my-block', [
'data' => [
'field_name' => 'the value you want to see by default',
]
]]
];
I hope it helps you too as well!
I’m stuck with repeater subfields now.
Didn’t find a way to fill them yet.
If any of you able to do that, don’t hesitate to write here about that. 😉
Bests,
Christian
You must be logged in to reply to this topic.
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!
Reminder that tomorrow we are talking about developing themes with #Gutenberg with @BillErickson for our latest #WPCafe episode.https://t.co/IN2z2f0WSp
— Mark Wilkinson (@wpmark) January 11, 2021
🗓 Tuesday 12th January 2021
⏰ 13:00
© 2021 Advanced Custom Fields. Subscribe
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.