HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
@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%;
}
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 {post._embedded.author[0].name}</a>
<a className="button read-more" href={post.link}>Read More »</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"));
Also see: Tab Triggers