Created
June 22, 2020 17:26
-
-
Save scottopolis/14b386d612fa4e55d0b5299248cc9110 to your computer and use it in GitHub Desktop.
AppPresser add Featured Images to Post Detail View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: AppPresser Featured Images | |
Plugin URI: http://apppresser.com | |
Description: Add featured images to single posts in the app. | |
Version: 0.1 | |
Author: AppPresser Team | |
Author URI: https://apppresser.com | |
License: GPLv2 | |
*/ | |
/** | |
* Use an AppPresser template hook | |
*/ | |
add_action( 'rest_api_init', 'sb_register_template_hook' ); | |
function sb_register_template_hook() { | |
register_rest_field( 'post', // any post type registered with API | |
'appp', | |
array( | |
'get_callback' => 'sb_get_hook_data', | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
} | |
/** | |
* Get the value of a meta field field | |
* | |
* @param array $object Details of current post. | |
* @param string $field_name Name of field. | |
* @param WP_REST_Request $request Current request | |
* | |
* @return mixed | |
*/ | |
function sb_get_hook_data( $object, $field_name, $request ) { | |
$data = []; | |
// uncomment these lines to add the post date | |
// $data['post_detail']['below_title'] = '<div class="post-date">' . get_the_time('F j, Y', $object['id']) . '</div>'; | |
// $data['post_list']['below_title'] = '<div class="post-date">' . get_the_time('F j, Y', $object['id']) . '</div>'; | |
// this adds the post thumbnail to post detail views | |
$data['post_detail']['above_title'] = '<div class="post-featured-wrap">' . get_the_post_thumbnail( $object['id'], 'large', array( 'class' => 'post-featured' ) ) . '</div>'; | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment