Home Forums Gutenberg Retrieve the (Gutenberg) block ID


Retrieve the (Gutenberg) block ID

  • How do I retrieve the ID of a (Gutenberg) block? The below code doesn’t echo the ID unfortunately.

        // Retrieve the current post object
        $current_post = get_post();
        // Retrieve all ACF blocks for the current post
        $acf_blocks = parse_blocks($current_post->post_content);
        // Loop through all ACF blocks to find the blocks with the desired fields
        foreach ($acf_blocks as $block) {
            $block_name = $block['blockName'];
            if ($block_name === 'acf/numbered-list' || $block_name === 'acf/text-image') {
                $block_id = $block['id'];
                $block_fields = $block['attrs']['data'];
                // Check if the 'quick_link_show' field is true and display the 'quick_link_text' field value
                if ($block_fields['quick_link_show']) { ?>
                    <a class="btn btn-accent" href="#<?= $block_id; ?>" title="Bekijk <?= $block_fields['quick_link_text']; ?>">bekijk <?= $block_fields['quick_link_text']; ?></a>
                <?php }
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.