个性化阅读
专注于IT技术分析

WordPress的meta-box不保存和更新

function wporg_add_custom_box(){
    $screens = ['product', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id', // Unique ID
            'Select a Authors Name ', // Box title
            'wporg_custom_box_html', // Content callback, must be of type callable
            $screen                   // Post type
        );
    }
}
add_action('edit_form_after_title', 'wporg_add_custom_box');

function wporg_custom_box_html($post){
    $value = get_post_meta($post->ID, '_getwriter_id', true);
    ?>
    <select name="book_writer_id" id="book_writer_id" class="postbox">
        <?php
$type = 'book_authors'; // your post type
$args=array(
  'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post();  $id = get_the_ID();?>
     <option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
    </select>
    <?php } 
function wporg_save_postdata($post_id)
{
    if (array_key_exists('book_writer_id', $_POST)) {
        update_post_meta(
            $post_id, '_getwriter_id', $_POST['book_writer_id']
        );
    }
}
add_action('save_post', 'wporg_save_postdata');

#1


我没有在那里使用save_postdate函数, 为什么, 它不保存值和更新数据

function wporg_add_custom_box(){
    $screens = ['product', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id', // Unique ID
            'Select a Authors Name ', // Box title
            'wporg_custom_box_html', // Content callback, must be of type callable
            $screen                   // Post type
        );
    }
}
add_action('edit_form_after_title', 'wporg_add_custom_box');

function wporg_custom_box_html($post)
{
    $value = get_post_meta($post->ID, '_getwriter_id', true);
    ?>
    <select name="book_writer_id" id="book_writer_id" class="postbox">
        <?php
$type = 'book_authors'; // your post type
$args=array(
  'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post();  $id = get_the_ID();?>
     <option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
    </select>
    <?php
}

function wporg_save_postdata($post_id)
{
    if (array_key_exists('book_writer_id', $_POST)) {
        update_post_meta(
            $post_id, '_getwriter_id', $_POST['book_writer_id']
        );
    }
}
add_action('save_post', 'wporg_save_postdata'); 
赞(0)
未经允许不得转载:srcmini » WordPress的meta-box不保存和更新

评论 抢沙发

评论前必须登录!