functions – How to get postId from the argument $block when register_block_type()?

Question

I’m trying to register custom block type in the file functions.php of my block theme:

function render_custom_block($attributes, $content, $block) {
    return $block->available_context['postId'];
}
add_action('init', function() {
    register_block_type('mytheme/custom-block',
        array(
            'render_callback' => 'render_custom_block'
        )
    );
});

But instead of getting postId, I always get 1. When I try print_r($block), I see the property available_context is protected and context is empty.

So how to get postId from the $block argument ?

0
stckvrw 3 weeks 2022-11-07T12:50:15-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse