如何在编辑类别管理员wordpress中添加一个metabox



我正在尝试,但如果成功使用add_meta_box()我有以下代码:

function myplugin2_add_meta_box() {
$screens = array( 'category', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'cat',
__( 'Scroll infinito', 'myplugin2_textdomain' ),
'myplugin2_meta_box_callback',
$screen,
'side'
);
}
}
add_action( 'add_meta_boxes', 'myplugin2_add_meta_box', 2 );
function myplugin2_meta_box_callback(){
global $post;
$custom = get_post_custom($post->ID);

?>
<label>Activar</label>

<?php
}

您可以尝试此代码。希望能成功。

<?php
function wp_category_fields($term) {

$short_description = get_term_meta($term->term_id, 'short_description', true);
$color_code = get_term_meta($term->term_id, 'color_code', true);
?>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[short_description]"><?php _e('Short description'); ?></label></th>
<td>
<textarea class="large-text" cols="50" rows="5" id="term_fields[short_description]" name="term_fields[short_description]"><?php echo esc_textarea($short_description); ?></textarea><br/>
<span class="description"><?php _e('Please enter short description'); ?></span>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row"><label for="term_fields[color_code]"><?php _e('Color code'); ?></label></th>
<td>
<input type="text" size="40" value="<?php echo esc_attr($color_code); ?>" id="term_fields[color_code]" name="term_fields[color_code]"><br/>
<span class="description"><?php _e('Please enter color hex code'); ?></span>
</td>
</tr>   
<?php
}
// Add the fields, using our callback function  
// if you have other taxonomy name, replace category with the name of your taxonomy. ex: book_add_form_fields, book_edit_form_fields
add_action('category_add_form_fields', 'wp_category_fields', 10, 2);
add_action('category_edit_form_fields', 'wp_category_fields', 10, 2);
function wp_save_category_fields($term_id) {
if (!isset($_POST['term_fields'])) {
return;
}
foreach ($_POST['term_fields'] as $key => $value) {
update_term_meta($term_id, $key, sanitize_text_field($value));
}
}
// Save the fields values, using our callback function
// if you have other taxonomy name, replace category with the name of your taxonomy. ex: edited_book, create_book
add_action('edited_category', 'wp_save_category_fields', 10, 2);
add_action('create_category', 'wp_save_category_fields', 10, 2);

显示值。

$id = get_queried_object_id();
echo get_term_meta($id, 'color_code', true);

最新更新