wpseek.com
A WordPress-centric search engine for devs and theme authors



render_block_core_post_excerpt › WordPress Function

Sincen/a
Deprecatedn/a
render_block_core_post_excerpt ( $attributes, $content, $block )
Parameters: (3)
  • (array) $attributes Block attributes.
    Required: Yes
  • (string) $content Block default content.
    Required: Yes
  • (WP_Block) $block Block instance.
    Required: Yes
Returns:
  • (string) Returns the filtered post excerpt for the current post wrapped inside "p" tags.
Defined at:
Codex:

Renders the `core/post-excerpt` block on the server.



Source

function render_block_core_post_excerpt( $attributes, $content, $block ) {
	if ( ! isset( $block->context['postId'] ) ) {
		return '';
	}

	$more_text = isset( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . $attributes['moreText'] . '</a>' : '';

	$filter_excerpt_length = function() use ( $attributes ) {
		return isset( $attributes['wordCount'] ) ? $attributes['wordCount'] : 55;
	};
	add_filter(
		'excerpt_length',
		$filter_excerpt_length
	);

	$classes = '';
	if ( isset( $attributes['textAlign'] ) ) {
		$classes .= 'has-text-align-' . $attributes['textAlign'];
	}
	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );

	$content = '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
	if ( ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'] ) {
		$content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
	} else {
		$content .= " $more_text</p>";
	}

	remove_filter(
		'excerpt_length',
		$filter_excerpt_length
	);

	return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
}