I am using ACF to create blocks for Gutenberg.
I have the folder structure
/blocks/block_name/block-file.php
/blocks/wf-block-start.php
/blocks/wf-block-end.php
in block-file.php, I have it coded as so:
get_template_part('blocks/wf-block-start');
// ACF Repeater fields here
get_template_part('blocks/wf-block-end');
This works fine and returns the repeater rows within block-file.php (removed to make it easier to read).
In my wf-block-start.php file (included via get_template_part), it is coded as so:
$the_id = get_the_ID();
$wf_settings_width = get_field('wf_settings_container_size', $the_id);
echo $wf_settings_width;
echo get_field('wf_settings_container_size', $the_id);
echo get_field('wf_settings_container_size');
None of the values are returning from get_field on the front end from files included using get_template_part.
When doing the following within wf-block-start.php, it returns data just fine:
global $post;
print_r($post); // Works fine
echo $post->ID; // Works fine
echo get_the_ID(); // Works fine
echo get_field('test_field'); // Doesn't work
echo get_field('test_field', $post->ID); // Doesn't work
echo get_field('test_field', get_the_ID()); // Doesn't work
// Test field in Options
print_r(get_field('test_field','options')); // Doesn't work
What’s strange is that it works perfectly fine when editing via block editor – the fields return correctly.
Page.php (excluding header/footer):
<?php if (have_posts()) while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Any ideas?
Ok, I have just realised that I am trying to obtain block data, rather than post data that does not exist. Silly me.
My next question is, how do I get the block data, when it isn’t returning any via get_template.
I am building a set of global templates for the blocks which is why I am including them, rather than having the code directly in the template.
Solved. Using the variable $post_id returns the correct data for that block.
$wf_settings_width = get_field('wf_settings_container_size', $post_id);'
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!
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.