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


Generated : Tue Dec 24 08:20:01 2024 Cross-referenced by PHPXref