Get Only Headings with Kirby Blocks

Hey all,

I’m creating a table of contents for my Kirby Blocks field. I am wondering if there is a way to only get the headings (h1, h2, h3, etc) from the blocks field so that I can put them as the href in the table of contents links and as the corresponding ID of the blocks section.

One solution would be to get the level of each block and filter them using an if statement to only get the levels with headings, but I am wondering if there is a simpler way.

Also, I noticed that in Kirby Blocks all headings are outputted as general HTML tags. This is not a massive problem, but if your site has multiple h1 tags, it does affect SEO to a degree.

All tips and help is very much appreciated, thanks :slight_smile:

You can filter blocks by type:

Thanks, here’s the snippet I used in case it is helpful to anyone:

$headings = $blocks->filterBy('type', 'heading');
1 Like