试试以下代码。它会添加一个文本框,允许输入将在帖子中显示的图片网址。使用下面的代码并相应地更改您的文章类型。你也可以任意数量的字段。
add_action('admin_init','add_metabox_post_banner_image_widget');
add_action('save_post','save_metabox_post_banner_image_widget');
/*
* Funtion to add a meta box to enable banner image widget on posts.
*/
function add_metabox_post_banner_image_widget()
{
add_meta_box("banner_image", "Enable Banner Image", "enable_post_banner_image_widget", "post", "normal", "high"); /* replace "post" with your custom post value(eg: "motors") */
}
function enable_post_banner_image_widget(){
global $post;
$image=get_post_custom($post->ID);
//print_r($image);
$banner_image_src = $image['post_banner_image_src'][0];
?>
<label for="post_banner_image_src">Banner Image Url:</label>
<input type="text" name="post_banner_image_src" id="post_banner_image_src" value="<?php if($banner_image_src!=''){echo $banner_image_src; } ?>" >
<p><em>Example: https://website.com/wp-content/uploads/2016/06/google.jpg</em></p>
<?php
}
/*
* Save the meta box value of banner image widget on posts.
*/
function save_metabox_post_banner_image_widget($post_id)
{
// Bail if we're doing an auto save
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// if our current user can't edit this post, bail
if(!current_user_can('edit_post')) return;
$banner_image_src = isset($_POST['post_banner_image_src']) ? $_POST['post_banner_image_src']:'';
update_post_meta($post_id, 'post_banner_image_src', $banner_image_src);
}
这篇文章应该包含你要查找的内容:http://wptheming.com/2010/08/custom-metabox-for-post-type/ – Peter