Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created May 30, 2019 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/a1be1700388f1b8fd62dd2ed5579567f to your computer and use it in GitHub Desktop.
Save billerickson/a1be1700388f1b8fd62dd2ed5579567f to your computer and use it in GitHub Desktop.
<?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