Skip to content

Commit

Permalink
Theming with WP REST API - Part 15 - Add Image To Post Type
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Doric committed Apr 26, 2016
1 parent ffdbb44 commit ad67068
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions functions.php
Expand Up @@ -117,11 +117,27 @@ function register_custom_fields(){
'get_callback' => 'show_fields'
)
);

register_rest_field(
'movies',
'thumbnail',
array(
'get_callback' => 'show_image'
)
);
}

function show_fields($object, $field_name, $request){
$field_name = 'wpcf-' . $field_name;
return get_post_meta($object['id'], $field_name, true);
}

function show_image($object, $field_name, $request){
$thumbID = get_post_thumbnail_id($object['id']);
$image = wp_get_attachment_image_src( $thumbID, '300x180');
return $image[0];
}




4 changes: 4 additions & 0 deletions index.php
Expand Up @@ -165,6 +165,10 @@
<div class="container">
<div class="post-list">
<article v-for="movie in movies" class="post">
<div class="image">
<img v-bind:src="movie.thumbnail">
</div>

<div class="post-content">
<h2>{{ movie.title.rendered }}</h2>
{{ movie.year }}
Expand Down

0 comments on commit ad67068

Please sign in to comment.