Skip to content

Instantly share code, notes, and snippets.

@miminari
Last active October 16, 2018 06:25
Show Gist options
  • Save miminari/148afd17e99930818f73ef7cce9fa5dc to your computer and use it in GitHub Desktop.
Save miminari/148afd17e99930818f73ef7cce9fa5dc to your computer and use it in GitHub Desktop.
get colors from wp rest api
const getColor = (posts) => {
for (const key in posts) {
if (posts[key]._embedded['wp:featuredmedia']) { // キャッチ画像が設定されている場合
var imgUrl = posts[key]._embedded['wp:featuredmedia'][0].media_details.sizes.thumbnail.source_url;
RGBaster.colors(imgUrl, { // eslint-disable-line
paletteSize: 3,
success: function (colors) {
Vue.set(vm.posts[key], 'color', colors.secondary);// eslint-disable-line
Vue.set(vm.posts[key], 'isLoaded', true);// eslint-disable-line
}
});
} else {
Vue.set(vm.posts[key], 'color', '#eee');// eslint-disable-line
Vue.set(vm.posts[key], 'isLoaded', true);// eslint-disable-line
}
}
};
<article class="m-card" v-bind:id="'post-' + post.id" v-for="post in posts">
<div class="m-media">
<a v-bind:href="post.link" class="a-color__container" v-bind:class="{'is-loaded' : post.isLoaded }">
<span class="a-color" v-bind:style="{ background: post.color }"></span>
<span v-if="post._embedded['wp:featuredmedia']"><img :src="post._embedded['wp:featuredmedia'][0].source_url" alt=""></span>
</a>
</div>
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment