Extract one block from post content

For instances where you perhaps want to extract a block out of a typical layout (say a hero when the following content is laid out differently).

function display_hero() {
     global $post;
     $blocks = parse_blocks( $post->post_content );
     foreach( $blocks as $block ) {
          if( 'acf/page-header' === $block['blockName'] ) {
               echo render_block( $block );
          break;
          }
     }
}

To then display the remaining content WITHOUT that block, utilize:

<?php
$blocks = parse_blocks( $post->post_content );
						
foreach ( $blocks as $block ) {
	if ($block['blockName'] !== 'acf/page-header') {
		echo render_block($block);
	}
} ?>

Leave a Reply

Your email address will not be published. Required fields are marked *