concrete5 pages can have several different areas where blocks can be added. Developers can programmatically get an array of all of the block objects in an area. In this recipe, we will load a page and get a list of all of the blocks in its main content area.
We will be using /config/site_post.php
to run some arbitrary code here. You can place this code wherever you find appropriate, though.
This example assumes the presence of a page with a path of /about
, and with a content area called content
. Make the necessary adjustments in the code as needed.
The steps for getting the blocks from an area are as follows:
Open
/config/site_post.php
in your code editor.Load the page by its path:
$page = Page::getByPath('/about');
Get the array of blocks in the page's content area.
$blocks = $page->getBlocks('content');
Loop through the array, printing each block's handle.
foreach ($blocks as $block) { echo $block->getBlockTypeHandle().'<br...