Last active
April 23, 2020 21:11
-
-
Save jonschr/f56701aebf858fb0733ebc0f49e8b478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function genesis_sample_blocks_body_classes( $classes ) { | |
if ( ! is_singular() || ! function_exists( 'has_blocks' ) || ! function_exists( 'parse_blocks') ) { | |
return $classes; | |
} | |
if ( ! has_blocks() ) { | |
$classes[] = 'has-no-blocks'; | |
return $classes; | |
} | |
$post_object = get_post( get_the_ID() ); | |
$blocks = (array) parse_blocks( $post_object->post_content ); | |
// foreach( $blocks as $block ) { | |
// echo 'Name: ' . $block['blockName'] . '<br/>'; | |
// echo 'Alignment: ' . $block['attrs']['align'] . '<br/>'; | |
// echo '<br/>'; | |
// } | |
//* Output the "first" classes | |
if ( isset( $blocks[0]['blockName'] ) ) { | |
$classes[] = 'first-block-' . str_replace( '/', '-', $blocks[0]['blockName'] ); | |
} | |
if ( isset( $blocks[0]['attrs']['align'] ) ) { | |
$classes[] = 'first-block-align-' . $blocks[0]['attrs']['align']; | |
} | |
//* Get the "last" classes | |
foreach ( $blocks as $block ) { | |
$lastclassname = null; | |
$lastclassalign = null; | |
if ( $block['blockName'] ) | |
$lastclassname = 'last-block-' . str_replace( '/', '-', $block['blockName'] ); | |
if ( $block['attrs']['align'] ) | |
$lastclassalign = 'last-block-align' . str_replace( '/', '-', $block['attrs']['align'] ); | |
} | |
//* Output the "last" classes | |
if ( isset( $lastclassname ) ) | |
$classes[] = $lastclassname; | |
if ( isset( $lastclassalign ) ) | |
$classes[] = $lastclassalign; | |
return $classes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment