Customizing the rendered output

Melonpan Block – Post List comes with a filter to modify the front end output: mbpl_content_to_render.

The filter callback will receive the following arguments:

  • $content The plugin passes an empty string. If other plugins/themes use the filter, this string would contain their returned output.
  • $items Array of post items. Each item is an array composed of post_type and post_id.
  • $attributes Array of attributes. className, align and posts_list (this last one is actually the property passed as the second argument of the callback).
/**
 * Modify Melonpan Block - Post List HTML output.
 */
add_filter( 'mbpl_content_to_render', 'my_theme_modify_mbpl_output', 10, 3 );
function my_theme_modify_mbpl_output( $content, $items, $attributes ) {

	// In this example we are generating a custom 'ul' element.
	// We ignore any previous modification of $content.
	$content = '';

	foreach ( $items as $key => $item ) {

		$post_type = $item['post_type'];
		$post_id   = $item['post_id'];

		if (
			empty( $post_id ) ||
			// Post is no longer available or isnt published.
			'publish' !== get_post_status( $post_id ) ||
			// Post type is no longer available.
			empty( get_post_type_object( $post_type ) )
		) {
			continue;
		}

		$content .= '<li class="entry">';
		$content .= '<a href="' . esc_url( get_permalink( $post_id ) ) . '">';
		$content .= '<div class="entry-image-container">';
		$content .= '<div class="entry-image-inner">';
		$content .= get_the_post_thumbnail( $post_id );
		$content .= '</div>';
		$content .= '</div>';
		$content .= '<div class="entry-title">';
		$content .= '<h3>' . esc_attr( get_the_title( $post_id ) ) . '</h3>';
		$content .= '<span>' . esc_attr( get_the_excerpt( $post_id ) ) . '</span>';
		$content .= '</div>';
		$content .= '</a>';
		$content .= '</li>';

	}

	$classes   = array();
	$classes[] = 'wp-block-melonpan-block-post-list';

	if ( ! empty( $attributes['className'] ) ) {
		$classes[] = $attributes['className'];
	}

	if ( ! empty( $attributes['align'] ) ) {
		$classes[] = 'align-' . $attributes['align'];
	}

	$content = '<ul class="' . esc_attr( join( ' ', $classes ) ) . '">' . $content . '</ul>';

	return $content;
}
PHP