1

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 ?

2
  • What does grt_the_ID() function return?
    – Tim
    Commented Nov 11, 2022 at 17:09
  • @Tim Yes, get_the_ID() returns the expected ID. I found that out a few days ago, but just was not updated the question
    – stckvrw
    Commented Nov 12, 2022 at 0:04

1 Answer 1

2

The get_the_ID() function will return the id of the current post in the loop. If your block is used on a single post page or in a post loop (archive page) you can use this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.