我正在尝试在自定义帖子类型中创建一个metabox输入字段,即使我尽可能按照说明操作,它也无法工作。我不是一个PHP开发人员,所以我想这可能只是一个小东西,它丢失了,或者它是错误的。
我的意思是,我根本不在WP UI中显示。
代码如下:
<?php
function add_post_type($name, $args = array()) {
add_action(\'init\', function() use($name, $args) {
$upper = ucwords($name);
$name = strtolower(str_replace(\' \',\'_\',$name));
$args = array_merge(
array(
\'public\'=> true,
\'label\' => "All $upper" . \'s\',
\'labels\' => array(\'add_new_item\' => "Add New $upper"),
\'support\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\'),
\'taxonomies\' => array(\'post_tag\',\'category\')
),
$args
);
register_post_type(\'$name\', $args);
});
}
//now we create and register a taxonomy
function add_taxonomy($name, $post_type, $args = array()) {
$name = strtolower($name);
add_action(\'init\', function() use($name, $post_type, $args) {
$args = array_merge(
array(
\'label\' => ucwords($name),
),
$args
);
register_taxonomy($name, $post_type, $args);
});
}
/************************************************************
Now we add the names of the custom post type and taxonomies
*************************************************************/
add_post_type(\'snippet\', array(
\'supports\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\'),
\'taxonomies\' => array(\'post_tag\')
));
add_taxonomy(\'language\', \'snippet\');
/************************************************************
Creating Metaboxes
*************************************************************/
add_action(\'add_meta_boxes\', function() {
add_meta_box(
\'er_snippet_info\',
\'Snippet Info\',
\'er_snippet_info_cb\',
\'snippet\',
\'normal\',
\'high\'
);
});
function er_snippet_info_cb() {
global $post;
$url = get_post_custom($post->ID);
?>
<label for="er_associated_url">Associated URL: </label>
<input type="text" name="er_associated_url" id="er_associated_url" value="<?php echo $url; ?>" />
<?php
}
add_action(\'save_post\', function () {
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;
//security check
if ($_POST && !wp_verify_nonce($_POST[\'er_nonce\'], _FILE_)) {
if ( isset ($_POST[\'er_associated_url\']) ) {
update_post_meta($post->ID, \'er_associated_url\', $_POST[\'er_associated_url\']);
}
}
});
?>
另一个问题是,即使函数存在,标记也没有命名。