Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active May 30, 2019 21:04
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/e6056eb24cd3826aa212b760c3d555e3 to your computer and use it in GitHub Desktop.
Save billerickson/e6056eb24cd3826aa212b760c3d555e3 to your computer and use it in GitHub Desktop.
<?php
/**
* List post headings
* @see https://www.billerickson.net/access-gutenberg-block-data/
*/
function be_list_post_headings() {
global $post;
$blocks = parse_blocks( $post->post_content );
$headings = array();
foreach( $blocks as $block ) {
if( 'core/heading' === $block['blockName'] )
$headings[] = wp_strip_all_tags( $block['innerHTML'] );
}
if( !empty( $headings ) ) {
echo '<ol class="table-of-contents">';
foreach( $headings as $heading )
echo '<li>' . $heading . '</li>';
echo '</ol>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment