Skip to content

Instantly share code, notes, and snippets.

@dacmail
Last active April 21, 2022 14:13
Show Gist options
  • Save dacmail/bfca3733827f3375c7f7ba4dfb655329 to your computer and use it in GitHub Desktop.
Save dacmail/bfca3733827f3375c7f7ba4dfb655329 to your computer and use it in GitHub Desktop.
Get ACF field value from Gutenberg Block
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;
}
global $post;
get_block_data($post, 'acf/image-text', 'title');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment