Skip to content

Instantly share code, notes, and snippets.

@jonschr

jonschr/init.php

Last active Apr 23, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.