-
Notifications
You must be signed in to change notification settings - Fork 0
/
myblock.js
39 lines (33 loc) · 1.04 KB
/
myblock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
withSelect = wp.data.withSelect;
registerBlockType( 'my-first-dynamic-gutenberg-block/latest-post', {
title: 'Latest Post Custom Block',
icon: 'megaphone',
category: 'widgets',
/*Edit can be rendered using PHP with ServerSideRender.
See https://wordpress.org/gutenberg/handbook/blocks/creating-dynamic-blocks*/
edit: withSelect( function( select ) {
return {
posts: select( 'core' ).getEntityRecords( 'postType', 'post' )
};
} )( function( props ) {
if ( ! props.posts ) {
return "Loading...";
}
if ( props.posts.length === 0 ) {
return "No posts";
}
var className = props.className;
var post = props.posts[ 0 ];
return el(
'a',
{ className: className, href: post.link },
post.title.rendered
);
} ),
save: function(props) {
// Rendering in PHP
return null;
},
} );