[ 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 supports Archive title, Search title, and Post Type Label,
  11   * using queried object information
  12   *
  13   * @since 5.8.0
  14   *
  15   * @param array  $attributes Block attributes.
  16   * @param array  $_content   Block content.
  17   * @param object $block      Block instance.
  18   *
  19   * @return string Returns the query title based on the queried object.
  20   */
  21  function render_block_core_query_title( $attributes, $content, $block ) {
  22      $type       = isset( $attributes['type'] ) ? $attributes['type'] : null;
  23      $is_archive = is_archive();
  24      $is_search  = is_search();
  25      $post_type  = isset( $block->context['query']['postType'] ) ? $block->context['query']['postType'] : get_post_type();
  26  
  27      if ( ! $type ||
  28          ( 'archive' === $type && ! $is_archive ) ||
  29          ( 'search' === $type && ! $is_search ) ||
  30          ( 'post-type' === $type && ! $post_type )
  31      ) {
  32          return '';
  33      }
  34      $title = '';
  35      if ( $is_archive ) {
  36          $show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
  37          if ( ! $show_prefix ) {
  38              add_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
  39              $title = get_the_archive_title();
  40              remove_filter( 'get_the_archive_title_prefix', '__return_empty_string', 1 );
  41          } else {
  42              $title = get_the_archive_title();
  43          }
  44      }
  45      if ( $is_search ) {
  46          $title = __( 'Search results' );
  47  
  48          if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {
  49              $title = sprintf(
  50                  /* translators: %s is the search term. */
  51                  __( 'Search results for: "%s"' ),
  52                  get_search_query()
  53              );
  54          }
  55      }
  56      if ( 'post-type' === $type ) {
  57          $post_type_object = get_post_type_object( $post_type );
  58  
  59          if ( ! $post_type_object ) {
  60              return '';
  61          }
  62  
  63          $post_type_name = $post_type_object->labels->singular_name;
  64          $show_prefix    = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
  65  
  66          if ( $show_prefix ) {
  67              $title = sprintf(
  68                  /* translators: %s is the post type name. */
  69                  __( 'Post Type: "%s"' ),
  70                  $post_type_name
  71              );
  72          } else {
  73              $title = $post_type_name;
  74          }
  75      }
  76  
  77      $level    = isset( $attributes['level'] ) ? (int) $attributes['level'] : 1;
  78      $tag_name = 0 === $level ? 'p' : 'h' . (int) $attributes['level'];
  79  
  80      $align_class_name   = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  81      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  82      return sprintf(
  83          '<%1$s %2$s>%3$s</%1$s>',
  84          $tag_name,
  85          $wrapper_attributes,
  86          $title
  87      );
  88  }
  89  
  90  /**
  91   * Registers the `core/query-title` block on the server.
  92   *
  93   * @since 5.8.0
  94   */
  95  function register_block_core_query_title() {
  96      register_block_type_from_metadata(
  97          __DIR__ . '/query-title',
  98          array(
  99              'render_callback' => 'render_block_core_query_title',
 100          )
 101      );
 102  }
 103  add_action( 'init', 'register_block_core_query_title' );


Generated : Wed Oct 22 08:20:04 2025 Cross-referenced by PHPXref