Support

Account

Home Forums Gutenberg get_field("block_fieldvalue") without ACF

Helping

get_field("block_fieldvalue") without ACF

  • I want to get $value = get_field(“block_fieldvalue”) without ACF.

    With multiple custom blocks on a page. If there’s two of the same custom block, I only get the value from the first block. Without the break; — I only get the value from the 2nd block.

    function getACF_block ( $field, $post_id = null) {

    $value = “”;
    $get_post = get_post($post_id);
    $blocks = parse_blocks( $get_post->post_content );

    foreach ( $blocks as $block ) {

    if( isset( $block[‘attrs’][‘data’] ) ) {

    $value = $block[‘attrs’][‘data’][$field];
    break;
    }

    }

    return $value;
    }

  • I believe Bill Erickson has some tutorials about retrieving block data. (Pause to look.) Yes! At the bottom of this article, he explains how to use parse_blocks on ACF blocks.

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

You must be logged in to reply to this topic.