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 Vue.js</h1>
<div class="post">
<h2 class="post-title">Latest Post Status Posts</h2>
<p>I wanted to show as simple of a Vue.js app as possible, using WordPress post data. I recently did something similar with React, which you can <a href="https://codepen.io/krogsgard/pen/NRBqPp/">check out</a>.</p>
<p>I used the Vue.js <a href="https://vuejs.org/examples/commits.html">Github commits example</a> as my primary guide for this demo.</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>, or WordPress Trunk (developer version).</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=</pre>
</code>
<p>The biggest change between this demo and the React demo is this one adds the author toggle, so you can get an idea for using more dynamic data.</p>
</div>
<div id="app">
<div class="author-toggle-wrap post">
<template v-for="author in authors">
<div class="author-toggle">
<input type="radio"
:id="author"
:value="author"
name="author"
v-model="currentAuthor">
<label :for="author">Author ID: {{ author }}</label>
</div>
</template>
</div>
<div class="post-wrapper">
<p>Current Author: <code>{{ currentAuthor }}</code></p>
<template v-for="post in posts">
<div class="post">
<h2 class="post-title" v-html="post.title.rendered"></h2>
<a v-if="post._embedded['wp:featuredmedia'][0].media_details.sizes['large']" :href="post.link">
<img :src="post._embedded['wp:featuredmedia'][0].media_details.sizes['large'].source_url" />
</a>
<div class="excerpt" v-if="post.excerpt.rendered" v-html="post.excerpt.rendered"></div>
<div class="entry-meta" v-if="post._embedded.author[0]">
<a class="author-wrap" :href="post._embedded.author[0].link"><img class="avatar" :src="post._embedded.author[0].avatar_urls['48']" />by {{ post._embedded.author[0].name }} </a>
<a class="button read-more" :href="post.link">Read More »</a>
</div>
</div>
</template>
</div>
</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. The fallback logic is probably better in this than the React version, because I was better able to understand <code>v-if</code> than React conditional logic, but I haven't spent much time with Vue.js at all, so there could be some issues here.</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;
}
.author-toggle-wrap {
display: block;
padding: 1em;
overflow: hidden;
}
.author-toggle {
display: inline-block;
width: 40%;
margin: 5%;
float: left;
margin-bottom: 1em;
}
.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 apiURL = 'https://poststatus.com/wp-json/wp/v2/posts/?_embed&per_page=3&author='
/**
* Posts demo with ability to change author
*/
var posts = new Vue({
el: '#app',
data: {
authors: ['1', '590'],
currentAuthor: '1',
posts: null
},
created: function() {
this.fetchData()
},
watch: {
currentAuthor: 'fetchData'
},
methods: {
fetchData: function() {
var xhr = new XMLHttpRequest()
var self = this
xhr.open('GET', apiURL + self.currentAuthor)
xhr.onload = function() {
self.posts = JSON.parse(xhr.responseText)
console.log(self.posts[0].link)
}
xhr.send()
}
}
})
Also see: Tab Triggers