I'm working with Advanced Custom Fields (ACF) to create custom blocks for the WordPress Gutenberg editor. I have designed a parent block named "Tabs" that can contain multiple inner blocks, each named "Tab".
Here's a simple representation of what I'm trying to achieve:
// Parent Block: Tabs (render.php file)
<Tabs>
<InnerBlocks />
</Tabs>
// InnerBlock: Tab (render.php file)
// Inside this block, I need to access the ID of the parent (Tabs)
// and also the current index or position of this inner block (Tab).
My challenge is to access:
- The ID of the parent block (Tabs) from inside each Tab inner block.
- The current index or position of the Tab inner block within the inner block itself.
Given that I'm using ACF for block creation, how might I retrieve this information in the render.php
file of my custom blocks?
Has anyone worked on something similar or can guide me through this?
Index
Maybe is not the better solution, but there is an approach In your Tab.tsx you can add this :
Of course you need to add index attributes when you register your component
Parent Id
For the parent id, below an approach to get the context of your parent
Parent Id #2
You have another approach that you can explore, it's to add context to Tabs.tsx
And in Tab.tsx recover the context
To read it in your Tab.tsx you can do this
Php Only
Provide Context
Provide your block id to child with the attribute