[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> query-title.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/query-title` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/query-title` block on the server.
  10   * For now it only supports Archive title,
  11   * using queried object information
  12   *
  13   * @param array $attributes Block attributes.
  14   *
  15   * @return string Returns the query title based on the queried object.
  16   */
  17  function render_block_core_query_title( $attributes ) {
  18      $type       = isset( $attributes['type'] ) ? $attributes['type'] : null;
  19      $is_archive = is_archive();
  20      $is_search  = is_search();
  21      if ( ! $type ||
  22          ( 'archive' === $type && ! $is_archive ) ||
  23          ( 'search' === $type && ! $is_search )
  24          ) {
  25          return '';
  26      }
  27      $title = '';
  28      if ( $is_archive ) {
  29          $show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
  30          if ( ! $show_prefix ) {
  31              add_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
  32              $title = get_the_archive_title();
  33              remove_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
  34          } else {
  35              $title = get_the_archive_title();
  36          }
  37      }
  38      if ( $is_search ) {
  39          $title = __( 'Search results' );
  40  
  41          if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {
  42              $title = sprintf(
  43                  /* translators: %s is the search term. */
  44                  __( 'Search results for: "%s"' ),
  45                  get_search_query()
  46              );
  47          }
  48      }
  49  
  50      $tag_name           = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
  51      $align_class_name   = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  52      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  53      return sprintf(
  54          '<%1$s %2$s>%3$s</%1$s>',
  55          $tag_name,
  56          $wrapper_attributes,
  57          $title
  58      );
  59  }
  60  
  61  /**
  62   * Registers the `core/query-title` block on the server.
  63   */
  64  function register_block_core_query_title() {
  65      register_block_type_from_metadata(
  66          __DIR__ . '/query-title',
  67          array(
  68              'render_callback' => 'render_block_core_query_title',
  69          )
  70      );
  71  }
  72  add_action( 'init', 'register_block_core_query_title' );


Generated : Fri Mar 29 08:20:02 2024 Cross-referenced by PHPXref