Hello,
Hope someone can help me with the following. I’m trying to make a blocks template in my functions.php. https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
I can add the acf/myblock without any problems, but can’t seem to fill the fields in data. How can I do that?
Thanks,
Noraly
Your question is not clear. Did you register your block to start in the preview mode? Then, when you load the block, you need to switch to edit mode. Or, if you registered the block in auto mode, then simply click on the block to automatically change it to the edit mode. Are you talking about having default values? You do that in the Field Groups when creating the fields… I can go on and on…
P.S Clicking the pencil icon on the toolbar will change the mode to edit, and when you click on the Monitor icon next, you will go back from the edit mode to the preview mode.
Follow the example found on this page: https://www.advancedcustomfields.com/resources/blocks/
and continue reading this: https://www.advancedcustomfields.com/resources/acf_register_block_type/
I hope you can solve your issues !
No i’m making a block template. So when the admin is creating a new page the prefered blocks are already there with the required settings already set. See link in original question.
Now the placement of the blocks core and acf is working when I click on new page, but how to set a specific field on a specific value.
For example on a page i want te use
Code/image
ACF/columns (set 2 columns)
ACF/columns (set 3 columns)
The field columns in the block columns need to get set in ‘data’ i think but I can’t find how.
Thanks,
Noraly
I had misunderstood your question… this is something I can use too… I will dedicate the next weekend to experiment with this, and I’ll post back here if I manage to find a solution. I’ve done 51 ACF blocks, bunch of Block Patterns (which are awesome and very easy to do), but never a template… time to dive into templates I guess now…
Hi, I’m not sure if this is what you are looking for but on my end it works great:
add_action( 'init', 'slug_post_type_template' );
function slug_post_type_template() {
$page_type_object = get_post_type_object( 'projects' );
$page_type_object->template = array(
array( 'acf/header', array() ),
array( 'acf/texts', array() ),
array( 'acf/photos', array() ),
);
}
You can check the source on this page:
https://florianbrinkmann.com/en/define-block-templates-5678/
I managed to do it!
I have an InnerBlocks
tag that I want to pre-load with a template. This template will create a custom acf block with pre-populated acf fields.
This is how my InnerBlocks Template looks like:
InnerBlocks_template = [
['acf/custom-block', { 'data' : {'field-name': 'This is my pre-loaded value'}}]
]
Templates can be declared in JS or in PHP as an array of blockTypes (block name and optional attributes).
The first example in PHP creates a template for posts that includes an image block to start, you can add as many or as few blocks to your template as needed.
PHP example:
<?php
function myplugin_register_template() {
$post_type_object = get_post_type_object( ‘post’ );
$post_type_object->template = array(
array( ‘core/image’ ),
);
}
add_action( ‘init’, ‘myplugin_register_template’ );
@ggus example worked for me, thank you!
<?php
$template = array(
array('acf/custom-block', array('data' => array('text_field'=> 'Test!'))),
);
?>
<div class="example-block">
<?php echo '<InnerBlocks template="' . esc_attr(wp_json_encode($template)) . '" />'; ?>
</div>
The topic ‘Block template’ is closed to new replies.
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.