Advertisement
pdrittenhouse

ACF < v5 Get Parent Block Data in Inner Block Template

Oct 30th, 2022 (edited)
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | Software | 0 0
  1. // Get parent block data
  2. function get_parent_block_data($needle, $haystacks, $name, &$block_data = array()) {
  3.   foreach ($haystacks as $haystack) {
  4.     if (!empty($haystack['attrs']['id']) && $haystack['attrs']['id'] == $needle) {
  5.       return $block_data;
  6.     }
  7.   }
  8.  
  9.   foreach ($haystacks as $haystack)  {
  10.     if (!empty($haystack['innerBlocks']) && $haystack['blockName'] !== $name) {
  11.  
  12.       $block_data = array();
  13.       foreach ($haystack['attrs']['data'] as $key => $value) {
  14.         if (substr($key, 0, 1) !== '_') {
  15.           $block_data[$key] = $value;
  16.         }
  17.       }
  18.       get_parent_block_data($needle, $haystack['innerBlocks'], $name, $block_data);
  19.     } else if (!empty($haystack['innerBlocks'])) {
  20.       get_parent_block_data($needle, $haystack['innerBlocks'], $name, $block_data);
  21.     }
  22.   }
  23. }
  24.  
  25. function get_parent_block($id, $name) {
  26.   $blocks = parse_blocks(get_post()->post_content);
  27.   get_parent_block_data($id, $blocks, $name, $block_data);
  28.   return $block_data;
  29. }
  30.  
  31. // Usage inside innerblock template
  32. $parent = get_parent_block(block['id'], block['name']);
  33. print_r($parent['FIELD_NAME']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement