[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> post-terms.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-terms` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/post-terms` block on the server.
  10   *
  11   * @param array    $attributes Block attributes.
  12   * @param string   $content    Block default content.
  13   * @param WP_Block $block      Block instance.
  14   * @return string Returns the filtered post terms for the current post wrapped inside "a" tags.
  15   */
  16  function render_block_core_post_terms( $attributes, $content, $block ) {
  17      if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) {
  18          return '';
  19      }
  20  
  21      if ( ! is_taxonomy_viewable( $attributes['term'] ) ) {
  22          return '';
  23      }
  24  
  25      $post_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
  26      if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
  27          return '';
  28      }
  29  
  30      $classes = 'taxonomy-' . $attributes['term'];
  31      if ( isset( $attributes['textAlign'] ) ) {
  32          $classes .= ' has-text-align-' . $attributes['textAlign'];
  33      }
  34  
  35      $separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator'];
  36  
  37      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  38  
  39      return get_the_term_list(
  40          $block->context['postId'],
  41          $attributes['term'],
  42          "<div $wrapper_attributes>",
  43          '<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
  44          '</div>'
  45      );
  46  }
  47  
  48  /**
  49   * Registers the `core/post-terms` block on the server.
  50   */
  51  function register_block_core_post_terms() {
  52      register_block_type_from_metadata(
  53          __DIR__ . '/post-terms',
  54          array(
  55              'render_callback' => 'render_block_core_post_terms',
  56          )
  57      );
  58  }
  59  add_action( 'init', 'register_block_core_post_terms' );


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1