在服务器上呈现核心/最新帖子块。

原型

render_block_core_latest_posts( array $attributes )

参数

$attributes (array) (Required) 块属性。

返回值

(string)  返回添加了最新帖子的帖子内容。

源文件

路径:wp-includes/blocks/latest-posts.php

<?php
...
function render_block_core_latest_posts( $attributes ) {
	$args = array(
		'numberposts' => $attributes['postsToShow'],
		'post_status' => 'publish',
		'order'       => $attributes['order'],
		'orderby'     => $attributes['orderBy'],
	);
	if ( isset( $attributes['categories'] ) ) {
		$args['category'] = $attributes['categories'];
	}
	$recent_posts = wp_get_recent_posts( $args );
	$list_items_markup = '';
	foreach ( $recent_posts as $post ) {
		$post_id = $post['ID'];
		$title = get_the_title( $post_id );
		if ( ! $title ) {
			$title = __( '(Untitled)' );
		}
		$list_items_markup .= sprintf(
			'<li><a href="%1$s">%2$s</a>',
			esc_url( get_permalink( $post_id ) ),
			esc_html( $title )
		);
		if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
			$list_items_markup .= sprintf(
				'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
				esc_attr( get_the_date( 'c', $post_id ) ),
				esc_html( get_the_date( '', $post_id ) )
			);
		}
		$list_items_markup .= "</li>n";
	}
	$class = 'wp-block-latest-posts';
	if ( isset( $attributes['align'] ) ) {
		$class .= ' align' . $attributes['align'];
	}
	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
		$class .= ' is-grid';
	}
	if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
		$class .= ' columns-' . $attributes['columns'];
	}
	if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
		$class .= ' has-dates';
	}
	if ( isset( $attributes['className'] ) ) {
		$class .= ' ' . $attributes['className'];
	}
	$block_content = sprintf(
		'<ul class="%1$s">%2$s</ul>',
		esc_attr( $class ),
		$list_items_markup
	);
	return $block_content;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/render_block_core_latest_posts/