Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Get parent block data
- function get_parent_block_data($needle, $haystacks, $name, &$block_data = array()) {
- foreach ($haystacks as $haystack) {
- if (!empty($haystack['attrs']['id']) && $haystack['attrs']['id'] == $needle) {
- return $block_data;
- }
- }
- foreach ($haystacks as $haystack) {
- if (!empty($haystack['innerBlocks']) && $haystack['blockName'] !== $name) {
- $block_data = array();
- foreach ($haystack['attrs']['data'] as $key => $value) {
- if (substr($key, 0, 1) !== '_') {
- $block_data[$key] = $value;
- }
- }
- get_parent_block_data($needle, $haystack['innerBlocks'], $name, $block_data);
- } else if (!empty($haystack['innerBlocks'])) {
- get_parent_block_data($needle, $haystack['innerBlocks'], $name, $block_data);
- }
- }
- }
- function get_parent_block($id, $name) {
- $blocks = parse_blocks(get_post()->post_content);
- get_parent_block_data($id, $blocks, $name, $block_data);
- return $block_data;
- }
- // Usage inside innerblock template
- $parent = get_parent_block(block['id'], block['name']);
- print_r($parent['FIELD_NAME']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement