Нема описа

post-featured-image.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-featured-image` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-featured-image` 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 featured image for the current post.
  14. */
  15. function render_block_core_post_featured_image( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) ) {
  17. return '';
  18. }
  19. $post_ID = $block->context['postId'];
  20. $featured_image = get_the_post_thumbnail( $post_ID );
  21. if ( ! $featured_image ) {
  22. return '';
  23. }
  24. if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  25. $featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image );
  26. }
  27. $wrapper_attributes = get_block_wrapper_attributes();
  28. return '<figure ' . $wrapper_attributes . '>' . $featured_image . '</figure>';
  29. }
  30. /**
  31. * Registers the `core/post-featured-image` block on the server.
  32. */
  33. function register_block_core_post_featured_image() {
  34. register_block_type_from_metadata(
  35. __DIR__ . '/post-featured-image',
  36. array(
  37. 'render_callback' => 'render_block_core_post_featured_image',
  38. )
  39. );
  40. }
  41. add_action( 'init', 'register_block_core_post_featured_image' );