Reposition Hero Block to Allow for Content Sidebar Layout when using Hero Block

<?php
/**
 * Move ACF Hero Block to 'genesis_after_header' if using a 'content-sidebar' layout
 *
 * @see https://whiteleydesigns.com/using-an-acf-hero-block-outside-of-the-post-content-for-pages-with-sidebars/
 *
 */
add_action( 'genesis_after_header', 'wd_isolate_hero_block' );
function wd_isolate_hero_block() {

     global $post;
     $count = 0;

     // Get all block content
     $blocks = parse_blocks( $post->post_content );

     // Get current page layout
     $layout = genesis_site_layout();

     // If no blocks are present return $classes
     if( $blocks ) {

          // Check if first block is hero block and layout is 'content-sidebar', then stop after first iteration
          foreach( $blocks as $block ) {
               if( $blocks[0]['blockName'] === 'acf/acf-hero-block' && $layout === 'content-sidebar' && $count < 1 ) {
                    echo render_block( $block );
                    $count++;

                    // Remove default entry content
                    remove_action( 'genesis_entry_content', 'genesis_do_post_content' );

                    // Replace entry content with hero block removed
                    add_action( 'genesis_entry_content', 'wd_render_blocks_without_hero' );

               }
          }

     }

}

/**
 * Render all blocks without hero block if hero block is first block and page template is 'content-sidebar'
 */
function wd_render_blocks_without_hero() {

     global $post;
     $count = 0;
     $content_markup = '';

     // Get all block content
     $blocks = parse_blocks( $post->post_content );

     // Get current page layout
     $layout = genesis_site_layout();

     // Loop over blocks and skip hero block if it is the first block and the page layout is content-sidebar
     foreach ( $blocks as $block ) {
          if ( 'acf/acf-hero-block' === $block['blockName'] && $count == 0 && $layout === 'content-sidebar' ) {
               continue;
          } else {
               $content_markup .= render_block( $block );
          }
     }
     echo apply_filters( 'the_content', $content_markup );

}

// Add custom body class if hero image is used on sidebar-content page
function wd_sidebar_hero_body_class( $classes ) {

     global $post;
     $count = 0;

     // Get all block content
     $blocks = parse_blocks( $post->post_content );

     // Get current page layout
     $layout = genesis_site_layout();

     // If no blocks are present return $classes
     if( !$blocks ) {
          return $classes;
     }

     // Check if first block is hero block and layout is 'content-sidebar', then stop after first iteration
     foreach( $blocks as $block ) {
          if( $blocks[0]['blockName'] === 'acf/acf-hero-block' && $layout === 'content-sidebar' && $count < 1 ) {
               $count++;
               $classes[] = 'sidebar-with-hero';
               return $classes;
          } else {
               return $classes;
          }
     }

}
add_filter( 'body_class', 'wd_sidebar_hero_body_class' );

About Matt Whiteley

Matt has been building bespoke WordPress websites for over 10 years specializing in the Genesis Framework. He integrates Advanced Custom Fields heavily in all builds, especially with the new Gutenberg Block Editor, allowing clients to easily modify all aspects of the their website with ease.

He works with a wide variety of small businesses and agencies across the country providing development services as well as hosting and maintenance. When he isn't coding he enjoys spending time with his wife and two children, golfing and playing poker.