css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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&nbsp; {{ post._embedded.author[0].name }} </a>
						<a class="button read-more" :href="post.link">Read More &raquo;</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()
		}
	}
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console