• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / twenty_twenty_one_print_first_instance_of_block() – Print the first instance of a block in the content, and then break away.

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;
}
 

 View on GitHub View on Trac

Published: 9th December 2020 | Last updated: 12th September 2021

Primary Sidebar

Information

Function name: twenty_twenty_one_print_first_instance_of_block
Plugin ref: Twenty Twenty-One
Version: 1.7
Sourcefile: inc/template-functions.php
File ref: inc/template-functions.php
Deprecated?: No
API Letters: F,O,P,T

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 6.1.1. PHP: 8.0.28
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2023. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites