Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LatestPosts] Fixes the excerpt length #20313

Merged
merged 7 commits into from
Mar 2, 2020
20 changes: 20 additions & 0 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @package WordPress
*/


/**
* Renders the `core/latest-posts` block on server.
*
Expand All @@ -13,6 +14,7 @@
* @return string Returns the post content with latest posts added.
*/
function render_block_core_latest_posts( $attributes ) {

$args = array(
'posts_per_page' => $attributes['postsToShow'],
'post_status' => 'publish',
Expand All @@ -21,6 +23,22 @@ function render_block_core_latest_posts( $attributes ) {
'suppress_filters' => false,
);

/**
* Callback for the excerpt_length filter used by
* the Latest Posts block at render time.
*
* @return int Returns the $length variable
* to allow the excerpt_length filter respect the Latest Block setting.
*/
$block_core_latest_posts_excerpt_length = function( $length ) use ( $attributes ) {
if ( ! empty( $attributes['excerptLength'] ) ) {
return $attributes['excerptLength'];
}
return $length;
};

add_filter( 'excerpt_length', $block_core_latest_posts_excerpt_length, 999 );

if ( isset( $attributes['categories'] ) ) {
$args['category'] = $attributes['categories'];
}
Expand Down Expand Up @@ -111,6 +129,8 @@ function render_block_core_latest_posts( $attributes ) {
$list_items_markup .= "</li>\n";
}

remove_filter( 'excerpt_length', $block_core_latest_posts_excerpt_length );

$class = 'wp-block-latest-posts wp-block-latest-posts__list';
if ( isset( $attributes['align'] ) ) {
$class .= ' align' . $attributes['align'];
Expand Down