[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> term-count.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/term-count` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/term-count` block on the server.
  10   *
  11   * @since 6.9.0
  12   *
  13   * @param array    $attributes Block attributes.
  14   * @param string   $content    Block default content.
  15   * @param WP_Block $block      Block instance.
  16   *
  17   * @return string Returns the count of the current taxonomy term wrapped inside a heading tag.
  18   */
  19  function render_block_core_term_count( $attributes, $content, $block ) {
  20      // Reuse the filtered term from Term Template to avoid a separate query for each count.
  21      if ( isset( $block->context['termData'] ) && $block->context['termData'] instanceof WP_Term ) {
  22          $term = $block->context['termData'];
  23      } elseif ( isset( $block->context['termId'] ) && isset( $block->context['taxonomy'] ) ) {
  24          $term = get_term( $block->context['termId'], $block->context['taxonomy'] );
  25      } else {
  26          $term = get_queried_object();
  27          if ( ! $term instanceof WP_Term ) {
  28              $term = null;
  29          }
  30      }
  31  
  32      if ( ! $term || is_wp_error( $term ) ) {
  33          return '';
  34      }
  35  
  36      $term_count = $term->count;
  37  
  38      // Format the term count based on bracket type.
  39      switch ( $attributes['bracketType'] ) {
  40          case 'none':
  41              // No formatting needed.
  42              break;
  43          case 'round':
  44              $term_count = "({$term_count})";
  45              break;
  46          case 'square':
  47              $term_count = "[{$term_count}]";
  48              break;
  49          case 'curly':
  50              $term_count = "{{$term_count}}";
  51              break;
  52          case 'angle':
  53              $term_count = "<{$term_count}>";
  54              break;
  55          default:
  56              // Default to no formatting for unknown types.
  57              break;
  58      }
  59  
  60      $wrapper_attributes = get_block_wrapper_attributes();
  61  
  62      return sprintf(
  63          '<div %1$s>%2$s</div>',
  64          $wrapper_attributes,
  65          $term_count
  66      );
  67  }
  68  
  69  /**
  70   * Registers the `core/term-count` block on the server.
  71   *
  72   * @since 6.9.0
  73   */
  74  function register_block_core_term_count() {
  75      register_block_type_from_metadata(
  76          __DIR__ . '/term-count',
  77          array(
  78              'render_callback' => 'render_block_core_term_count',
  79          )
  80      );
  81  }
  82  add_action( 'init', 'register_block_core_term_count' );


Generated : Thu Nov 5 08:20:37 2026 Cross-referenced by PHPXref