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
27 changes: 27 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,26 @@
* @package WordPress
*/

/**
* The excerpt length set by the Latest Posts core block
* set at render time and used by the block itself.
*
* @var int
*/
$block_core_latest_posts_excerpt_length = 0;

/**
* Callback for the excerpt_length filter used by
* the Latest Posts block at render time.
*
* @return int Returns the global $block_core_latest_posts_excerpt_length variable
* to allow the excerpt_length filter respect the Latest Block setting.
*/
function block_core_latest_posts_get_excerpt_length() {
global $block_core_latest_posts_excerpt_length;
return $block_core_latest_posts_excerpt_length;
}

draganescu marked this conversation as resolved.
Show resolved Hide resolved
/**
* Renders the `core/latest-posts` block on server.
*
Expand All @@ -13,6 +33,8 @@
* @return string Returns the post content with latest posts added.
*/
function render_block_core_latest_posts( $attributes ) {
global $block_core_latest_posts_excerpt_length;

draganescu marked this conversation as resolved.
Show resolved Hide resolved
$args = array(
'posts_per_page' => $attributes['postsToShow'],
'post_status' => 'publish',
Expand All @@ -21,6 +43,9 @@ function render_block_core_latest_posts( $attributes ) {
'suppress_filters' => false,
);

$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );

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

remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );

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