Açıklama Yok

post-terms.php 1.6KB

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