Search code examples
phpwordpresswordpress-gutenberggutenberg-blocks

Get Gutenberg Blocks without the_content()


I'm trying to display the gutenberg blocks from a specific post ID inside another one.

The question is, does exist a function that I can get all blocks from one post and display it anywhere in the site? Just like get_the_content do?


Solution

  • I think you may get the Blocks of the Gutenberg using this way.

    $post_id = 1;
    $post = get_post( $post_id ); 
    
    if ( has_blocks( $post->post_content ) ) {
        $blocks = parse_blocks( $post->post_content );
        print'<pre>';print_r($blocks);print'</pre>';
        foreach( $blocks as $block ) {
            echo render_block( $block );
        }
    }
    

    Note: I haven't tested the code by myself.