You appear to be a bot. Output may be restricted
Description
Print the first instance of a block in the content, and then break away.
Usage
$bool = twenty_twenty_one_print_first_instance_of_block( $block_name, $content, $instances );
Parameters
- $block_name
- ( string ) required – The full block type name, or a partial match. Example: `core/image`, `core-embed/*`.
- $content
- ( string|null ) optional – The content to search in. Use null for get_the_content().
- $instances
- ( int ) optional default: 1 – How many instances of the block will be printed (max). Default 1.
Returns
bool Returns true if a block was located & printed, otherwise false.
Source
File name: twentytwentyone/inc/template-functions.php
Lines:
1 to 51 of 51
function twenty_twenty_one_print_first_instance_of_block( $block_name, $content = null, $instances = 1 ) { $instances_count = 0; $blocks_content = ''; if ( ! $content ) { $content = get_the_content(); } // Parse blocks in the content. $blocks = parse_blocks( $content ); // Loop blocks. foreach ( $blocks as $block ) { // Sanity check. if ( ! isset( $block['blockName'] ) ) { continue; } // Check if this the block matches the $block_name. $is_matching_block = false; // If the block ends with *, try to match the first portion. if ( '*' === $block_name[-1] ) { $is_matching_block = 0 === strpos( $block['blockName'], rtrim( $block_name, '*' ) ); } else { $is_matching_block = $block_name === $block['blockName']; } if ( $is_matching_block ) { // Increment count. $instances_count++; // Add the block HTML. $blocks_content .= render_block( $block ); // Break the loop if the $instances count was reached. if ( $instances_count >= $instances ) { break; } } } if ( $blocks_content ) { /** This filter is documented in wp-includes/post-template.php */ echo apply_filters( 'the_content', $blocks_content ); // phpcs:ignore WordPress.Security.EscapeOutput return true; } return false; }