我使用WordPress Meta Box Generator v2 Beta网站生成了一个自定义的Meta Box,将自定义图像文件上传到自定义帖子中,在管理端,一切似乎都很好,但我不知道如何在前端进行输出。所以我尝试了<?php echo get_post_meta( get_the_id(), \'project_logo_logo-image\', true ); ?>
但似乎什么都不起作用,也没有任何产出。请你给我指一下正确的方向好吗?
class Rational_Meta_Box {
private $screens = array(
\'portfolio_post_type\',
);
private $fields = array(
array(
\'id\' => \'logo-image\',
\'label\' => \'Logo Image\',
\'type\' => \'media\',
),
);
/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( \'add_meta_boxes\', array( $this, \'add_meta_boxes\' ) );
add_action( \'admin_footer\', array( $this, \'admin_footer\' ) );
add_action( \'save_post\', array( $this, \'save_post\' ) );
}
/**
* Hooks into WordPress\' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
\'project-logo\',
__( \'Project Logo\', \'yopta\' ),
array( $this, \'add_meta_box_callback\' ),
$screen,
\'normal\',
\'default\'
);
}
}
/**
* Generates the HTML for the meta box
*
* @param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( \'project_logo_data\', \'project_logo_nonce\' );
echo \'Add Project Logo Image\';
$this->generate_fields( $post );
}
/**
* Hooks into WordPress\' admin_footer function.
* Adds scripts for media uploader.
*/
public function admin_footer() {
?><script>
// https://codestag.com/how-to-use-wordpress-3-5-media-uploader-in-theme-options/
jQuery(document).ready(function($){
if ( typeof wp.media !== \'undefined\' ) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$(\'.rational-metabox-media\').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr(\'id\').replace(\'_button\', \'\');
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$("#"+id).val(attachment.url);
} else {
return _orig_send_attachment.apply( this, [props, attachment] );
};
}
wp.media.editor.open(button);
return false;
});
$(\'.add_media\').on(\'click\', function(){
_custom_media = false;
});
}
});
</script><?php
}
/**
* Generates the field\'s HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = \'\';
foreach ( $this->fields as $field ) {
$label = \'<label for="\' . $field[\'id\'] . \'">\' . $field[\'label\'] . \'</label>\';
$db_value = get_post_meta( $post->ID, \'project_logo_\' . $field[\'id\'], true );
switch ( $field[\'type\'] ) {
case \'media\':
$input = sprintf(
\'<input class="regular-text" id="%s" name="%s" type="text" value="%s"> <input class="button rational-metabox-media" id="%s_button" name="%s_button" type="button" value="Upload" />\',
$field[\'id\'],
$field[\'id\'],
$db_value,
$field[\'id\'],
$field[\'id\']
);
break;
default:
$input = sprintf(
\'<input %s id="%s" name="%s" type="%s" value="%s">\',
$field[\'type\'] !== \'color\' ? \'class="regular-text"\' : \'\',
$field[\'id\'],
$field[\'id\'],
$field[\'type\'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo \'<table class="form-table"><tbody>\' . $output . \'</tbody></table>\';
}
/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
\'<tr><th scope="row">%s</th><td>%s</td></tr>\',
$label,
$input
);
}
/**
* Hooks into WordPress\' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST[\'project_logo_nonce\'] ) )
return $post_id;
$nonce = $_POST[\'project_logo_nonce\'];
if ( !wp_verify_nonce( $nonce, \'project_logo_data\' ) )
return $post_id;
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field[\'id\'] ] ) ) {
switch ( $field[\'type\'] ) {
case \'email\':
$_POST[ $field[\'id\'] ] = sanitize_email( $_POST[ $field[\'id\'] ] );
break;
case \'text\':
$_POST[ $field[\'id\'] ] = sanitize_text_field( $_POST[ $field[\'id\'] ] );
break;
}
update_post_meta( $post_id, \'project_logo_\' . $field[\'id\'], $_POST[ $field[\'id\'] ] );
} else if ( $field[\'type\'] === \'checkbox\' ) {
update_post_meta( $post_id, \'project_logo_\' . $field[\'id\'], \'0\' );
}
}
}
}
new Rational_Meta_Box;