-
-
Save billerickson/a1be1700388f1b8fd62dd2ed5579567f 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
<?php | |
/** | |
* Get service sections | |
* @see https://www.billerickson.net/access-gutenberg-block-data/ | |
*/ | |
function ea_get_service_sections() { | |
$sections = array(); | |
global $post; | |
$blocks = parse_blocks( $post->post_content ); | |
foreach( $blocks as $block ) { | |
if( 'acf/service' !== $block['blockName'] ) | |
continue; | |
$title = $anchor = ''; | |
if( !empty( $block['attrs']['data']['title'] ) ) | |
$title = $block['attrs']['data']['title']; | |
if( !empty( $block['attrs']['data']['anchor'] ) ) | |
$anchor = $block['attrs']['data']['anchor']; | |
if( empty( $anchor ) ) | |
$anchor = $title; | |
$sections[] = '<a href="' . get_permalink() . '#' . sanitize_title_with_dashes( $anchor ) . '">' . esc_html( $title ) . '</a>'; | |
} | |
if( empty( $sections ) ) | |
return; | |
echo '<ul class="service-sections">'; | |
foreach( $sections as $section ) | |
echo '<li>' . $section . '</li>'; | |
echo '</ul>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment