Hello!
I’ve been setting up ACF/Gutenberg following this guide https://www.advancedcustomfields.com/blog/acf-5-8-introducing-acf-blocks-for-gutenberg/ and have everything (nearly) working. I just can’t seem to get the values outputting on the frontend, I know it’s a bit vague but I’ve been pulling my hair out for hours and can’t find what’s wrong.
Here’s my setup:
<?php
/**
* Registers ACF blocks to be used with WP Gutenberg
*
* https://www.advancedcustomfields.com/blog/acf-5-8-introducing-acf-blocks-for-gutenberg/
*/
add_action('acf/init', 'register_acf_blocks');
function register_acf_blocks() {
// check function exists
if( function_exists('acf_register_block') ) {
// register a hero block
acf_register_block(array(
'name' => 'hero',
'title' => __('Hero'),
'description' => __('A hero banner block with optional headings overlaid'),
'render_callback' => 'acf_block_render_callback',
'category' => 'formatting',
'icon' => 'playlist-video',
'keywords' => array( 'hero', 'heading', 'video' ),
));
}
}
function acf_block_render_callback( $block ) {
// convert name ("acf/testimonial") into path friendly slug ("testimonial")
$slug = str_replace('acf/', '', $block['name']);
// include a template part from within the "/blocks" folder
if( file_exists(STYLESHEETPATH . "/blocks/{$slug}.php") ) {
include( STYLESHEETPATH . "/blocks/{$slug}.php" );
}
}
Backend setup
https://imgur.com/VZKjWgE
And the template output
<?php
/**
* Block Name: Hero
*
* This is the template that displays the hero block.
*/
$media = get_field('hero_media');
$media_path = $media['url'];
$media_type = $media['type'];
$show_headings = get_field('show_headings');
$heading = get_field('heading');
$subheading = get_field('subheading');
?>
<?php print_r(get_field_object('field_5c0fabfccbf96'); ?>
<section class="hero" <?php $media_type == 'image' ?: 'style="background-image: url('.$media_path.')"'; ?>>
<?php if ($media_type == 'video'): ?>
<video autoplay muted loop id="hero-video">
<source src="<?= $media_path ?>" type="video/mp4">
</video>
<?php endif; ?>
<?php if ($show_headings): ?>
<div class="container">
<h2><?= $heading ?></h2>
<h3><?= $subheading ?></h3>
</div>
<?php endif; ?>
</section>
And outputs
https://imgur.com/gAe3KdS
This shows it is outputting the block, but the values are empty. Can anyone shed some light on what I’m doing wrong?
Let me know if you need more details.
Thanks, Harry.
Problem solved!
So after playing around, I realised I was including the block in my template directly, rather than just using ‘the_content();’.
So my template just looks like this now (which makes so much sense!):
<?php
/** Template name: Home */
?>
<?php
get_template_part('blocks/header');
the_content();
get_template_part('blocks/footer');
?>
Hi
I am attempting a very similar thing to you but having the same problem. When I add a block, type some content into the editor, then deselect the block, and reselect it, the values I entered are missing.
I have stripped it all back to a really simple test in my functions.php file as follows:
acf_register_block(array(
'name' => 'my_test',
'title' => __('Test'),
'description' => __('A Test'),
'render_callback' => 'render_test',
));
function render_test() {
$title = get_field('title');
$content = get_field('content');
?>
<h2><?php echo $title ?></h2>
<?php echo $content ?>
<?php
}
My template is simply:
<?php get_header(); ?>
<?php the_content() ?>
<?php get_footer(); ?>
I am using 5.8-beta3. I am at a bit of a loss as to what is going on. If anyone could give me any suggestions, I would really appreciate it!!
Many thanks
David
The topic ‘Can't output values on frontend’ 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.