SO网友:François J.
下面是一个完整的示例Featured image meta box 在外部显示和工作wp-admin/post.php
页面上下文(自WordPress 4.6以来,请参见wp-admin/includes/post.php
), 这可能是您想知道代码存储在哪里的原因之一:
/**
* Featured image metabox
*
* @uses _wp_post_thumbnail_html
*
* @param int $post_id Post ID.
* @param string $post_type Post type.
*/
function featured_image_metabox( $post_id, $post_type ) {
// @see edit-form-advanced.php
$thumbnail_support = current_theme_supports( \'post-thumbnails\', $post_type ) && post_type_supports( $post_type, \'thumbnail\' );
if ( ! $thumbnail_support ||
! current_user_can( \'upload_files\' ) ) {
return;
}
// All meta boxes should be defined and added before the first do_meta_boxes() call (or potentially during the do_meta_boxes action).
require_once ABSPATH . \'wp-admin/includes/meta-boxes.php\';
if ( ! $post_id ) {
// New post.
$post = get_default_post_to_edit( $post_type, true );
// Fix PHP error. Add filter so get_post( $post ) returns our post!
$post->filter = \'raw\';
} else {
$post = get_post( $post_id );
}
$thumbnail_id = get_post_meta( $post->ID, \'_thumbnail_id\', true );
if ( ! $thumbnail_id ) {
$thumbnail_id = -1;
}
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->featured_image;
?>
<script>
jQuery( document ).ready(function( e ) {
wp.media.view.settings.post.id = <?php echo json_encode( $post->ID ); ?>;
wp.media.view.settings.post.featuredImageId = <?php echo json_encode( $thumbnail_id ); ?>;
wp.media.view.settings.post.nonce = <?php echo json_encode( wp_create_nonce( \'update-post_\' . $post->ID ) ); ?>;
});
</script>
<div id="poststuff" style="min-width: 320px;">
<div id="postimagediv" class="postbox">
<h2 class=\'hndle\'><span><?php echo esc_html( $title ); ?></span></h2>
<div class="inside">
<?php
echo _wp_post_thumbnail_html( $thumbnail_id, $post );
?>
</div>
</div>
</div>
<?php
}