Skip to content

Instantly share code, notes, and snippets.

@sayal322l
Last active December 10, 2024 16:23
Show Gist options
  • Save sayal322l/169d7de41647b65c7e902d2bbd89db00 to your computer and use it in GitHub Desktop.
Save sayal322l/169d7de41647b65c7e902d2bbd89db00 to your computer and use it in GitHub Desktop.
PHP
<php
function register_custom_post_type() {
register_post_type('slug-name', array(
'label' => 'ブログ',
'public' => true,
'show_in_rest' => true, // REST API を有効化
'supports' => array('title', 'editor', 'thumbnail'),
));
}
add_action('init', 'register_custom_post_type');
?>
<php
function add_thumbnail_to_rest_api($data, $post, $context) {
if (has_post_thumbnail($post->ID)) {
$data->data['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'full');
} else {
$data->data['thumbnail'] = get_template_directory_uri() . '/img-path';
}
return $data;
}
add_filter('rest_prepare_post', 'add_thumbnail_to_rest_api', 10, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment