Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                 <div id="main">
  <h1>WordPress Posts in React</h1>
	<div class="post">
		<h2 class="post-title">Latest Post Status Posts</h2>
	  <p>In an attempt to show as simple of a React app as possible, I am stealing <a href="http://mediatemple.net/blog/tips/loading-and-using-external-data-in-react/">Chris Coyier's example</a> that shows CodePen jobs. I wanted to show an example using WordPress post data.</p>
	  <p>If you fork this Pen, you can swap out the URL from Post Status to your own website, if it's running <a href="http://v2.wp-api.org">v2 of the WordPress REST API</a>.</p>
	  <p>The author and featured image data is made available thanks to the <code>?_embed</code> query string, which adds additional information to the return data: author, media, terms, and comments are all included, for example.</p>
	  <p>The query for the following posts looks like this: <br>
	  <pre>https://poststatus.com/wp-json/wp/v2/posts/?_embed&per_page=3&author=1</pre>
	  </code>
	 <p>To see something similar with Vue.js, check out <a href="https://codepen.io/krogsgard/pen/pEYxEG">this Pen</a>.</p>
	</div>	  
	<div id="app"></div>
	<div class="notes">
		<div class="post">
			<h2 class="post-title">Notes</h2>
			<p>For the featured image, if you don't want to reference a specific size, you could call <code>post._embedded['wp:featuredmedia'][0].source_url</code> instead of <code>post._embedded['wp:featuredmedia'][0].media_details.sizes["large"].source_url</code> and the full size image would be returned. You can also replace the large size with other image sizes. I used the syntax shown in case your image size has a hyphen in it.</p>
			<p>Additionally, you can change the post type to something like <code>pages</code> or a custom post type to see more results.</p>
			<p>To change other things, like <code>per_page</code> and <code>author</code>, just add or adjust the appropriate query parameters.</p>
			<p>There are a few potential "gotchas" if you run into an error. I don't have a lot of fallback logic in case the right data isn't available. If you have trouble, that's the most likely source of the issue.</p>
		</div>
	</div>
</div>

              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Nunito:300);

body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
	font-weight: 300;
	padding: 2em;
	color: #666;
}

pre {
	overflow: scroll;
	padding: 1em .5em;
	background: #f4f4f4;
}
#main {
	max-width: 30em;
	margin: 0 auto;
	word-wrap: break-word;
}

h1 {
	font-size: 2em;
	margin: 0;
	font-family: Nunito, sans-serif;
	color: #2e3641;
}

a {
	color: #fc781f;
}

.post {
	font-size: 14px;
	font-weight: 300;
	border-bottom: 1px solid #ddd;
	padding: 2em 0;
}

.post-title {
	display: block;
	font-family: Nunito, sans-serif;
	margin: 0 0 10px 0;
	font-size: 1.5em;
	margin-bottom: 1em;
	color: #2e3641;
	a {
		color: #2e3641;
		text-decoration: none;
		&:hover {
			color: #fc781f;
		}
	}
}

.excerpt {
	margin: 1em 0;
}

.entry-meta {
	display: block;
	text-transform: uppercase;
	font-family: Nunito, sans-serif;
	margin: 2em 0 1em;
	overflow: hidden;
	line-height: 2;
	.author-wrap {
		float: left;
		display: inline-block;
		padding: 0;
		text-decoration: none;
		color: #2e3641;
		&:hover {
			color: #fc781f;
		}
		span {
			display: inline-block;
			padding: .75em 0;
		}
		.avatar {
			float: left;
			border-radius: 50%;
			margin-right: .5em;
		}
	}
	.read-more {
		float: right;
	}
}

@media ( max-width: 25em ) {
	.entry-meta {
		font-size: .75em;
		.avatar {
			width: 32px;
			height: 32px;
		}
	}
}

a.button {
	display: inline-block;
	padding: .75em 1em;
	background: #fc781f;
	color: white;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	font-family: Nunito, sans-serif;
	max-width: 300px;
	margin: 0 auto;
}

img {
	max-width: 100%;
}
              
            
!

JS

              
                var App = React.createClass({

	getInitialState: function() {
		return {
			posts: []
		}
	},

	componentDidMount: function() {
		var th = this;
		this.serverRequest = 
		axios.get(this.props.source)
			.then(function(result) {    
				th.setState({
				posts: result.data
			});
		})
	},

	componentWillUnmount: function() {
		this.serverRequest.abort();
	},

	render: function() {
		return (
			<div className="post-wrapper">
			{this.state.posts.map(function(post) {
				return (
					<div key={post.link} className="post">
						<h2 className="post-title"><a href={post.link}
						dangerouslySetInnerHTML={{__html:post.title.rendered}}
						/></h2>
						{post.featured_media ?
							<a href={post.link}><img src={post._embedded['wp:featuredmedia'][0].media_details.sizes["large"].source_url} /></a>
						: null}
						{post.excerpt.rendered ?
							<div className="excerpt" dangerouslySetInnerHTML={{__html:post.excerpt.rendered}} />
						: null}
						<div className="entry-meta">
							<a className="author-wrap" href={post._embedded.author[0].link}><img className="avatar" src={post._embedded.author[0].avatar_urls['48']} />by&nbsp; {post._embedded.author[0].name}</a>
							<a className="button read-more" href={post.link}>Read More &raquo;</a>
						</div>
					</div>
				);
			})}
			</div>
		)
	}
	
});

React.render(<App source="https://poststatus.com/wp-json/wp/v2/posts/?_embed&per_page=3&author=1" />, document.querySelector("#app"));

              
            
!
999px

Console