Support

Account

Home Forums Gutenberg Get value of ACF field in Gutenberg block Reply To: Get value of ACF field in Gutenberg block

  • You can get acf custom field’s value from block’s data.

    function get_block_data($post, $block_name = 'core/heading', $field_name = "" ){
    	$content = "";
    	if ( has_blocks( $post->post_content ) && !empty($field_name )) {
    	    $blocks = parse_blocks( $post->post_content );
    	    foreach($blocks as $block){
    		    if ( $block['blockName'] === $block_name ) {
    		    	if(isset($block["attrs"]["data"][$field_name ])){
                       $content = $block["attrs"]["data"][$field_name ];
    		    	}
    		    }	    	
    	    }  
    	}
    	return $content;
    }