-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I also had to rename the block from core/latest-posts to core/latestposts since I think there's a bug in the server-side matcher -- it ignores block comments with hyphens: #882
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
function gutenberg_block_core_latest_posts( $attributes ) { | ||
$postsToShow = 5; | ||
|
||
if ( array_key_exists( 'poststoshow', $attributes ) ) { | ||
$postsToShow = $attributes['poststoshow']; | ||
} | ||
|
||
$recent_posts = wp_get_recent_posts( array( | ||
'numberposts' => $postsToShow, | ||
'post_status' => 'publish' | ||
) ); | ||
|
||
$posts_content = ''; | ||
|
||
foreach( $recent_posts as $post ) { | ||
$post_permalink = get_permalink( $post['ID'] ); | ||
|
||
$posts_content .= "<li><a href='{$post_permalink}'>{$post['post_title']}</a></li>\n"; | ||
} | ||
|
||
$block_content = <<<CONTENT | ||
<div class="blocks-latest-posts"> | ||
<ul> | ||
{$posts_content} | ||
</ul> | ||
</div> | ||
CONTENT; | ||
|
||
return $block_content; | ||
} | ||
|
||
register_block( 'core/latestposts', array( | ||
'render' => 'gutenberg_block_core_latest_posts' | ||
) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters