Last active
April 21, 2022 14:13
-
-
Save dacmail/bfca3733827f3375c7f7ba4dfb655329 to your computer and use it in GitHub Desktop.
Get ACF field value from Gutenberg Block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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