Skip to content

Commit

Permalink
Add server-side rendering
Browse files Browse the repository at this point in the history
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
lamosty committed Jun 7, 2017
1 parent 484506a commit ec72c14
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { registerBlock } from '../../api';
import { getLatestPosts } from './data.js';


registerBlock( 'core/latest-posts', {
registerBlock( 'core/latestposts', {
title: wp.i18n.__( 'Latest Posts' ),

icon: 'list-view',
Expand Down
37 changes: 37 additions & 0 deletions blocks/library/latest-posts/index.php
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'
) );

0 comments on commit ec72c14

Please sign in to comment.