Last active
March 15, 2018 12:15
-
-
Save DannyCooper/66e565a9811d38e1be0f6f97071797b9 to your computer and use it in GitHub Desktop.
First Block Attempt
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
/** | |
* Simple block to output a list of posts (as hyperlinked images). | |
* | |
* Simple block, renders and saves the same content without interactivity. | |
* | |
*/ | |
var el = wp.element.createElement, | |
registerBlockType = wp.blocks.registerBlockType, | |
withAPIData = wp.components.withAPIData; | |
registerBlockType( 'my-plugin/latest-post', { | |
title: 'Latest Post Images', | |
icon: 'megaphone', | |
category: 'widgets', | |
edit: withAPIData( function() { | |
return { | |
posts: '/traction/v1/posts/2' | |
}; | |
} )( function( props ) { | |
if ( ! props.posts.data ) { | |
return "loading !"; | |
} | |
if ( props.posts.data.length === 0 ) { | |
return "No posts"; | |
} | |
var className = props.className; | |
var posts = props.posts.data; | |
posts.forEach(function(post) { | |
return el( | |
'a', { className: className, href: post.guid }, | |
el( 'img', { src: post.featured_image } ) | |
); | |
}); | |
} ), | |
save: function() { | |
// Rendering in PHP | |
return null; | |
}, | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment