Support

Account

Home Forums Gutenberg ACF Fields not returning – ACF Block + get_template_part()

Solved

ACF Fields not returning – ACF Block + get_template_part()

  • 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);'

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.