保存Meta Box数据问题

时间:2013-12-10 14:51:09

标签: php wordpress save

我在我的wordpress保存元数据数据中有问题,我使用了一些教程,但它们对我没有帮助,我搜索了很多但没有用的

还告诉我哪里有元数据保存,我的意思是在哪个表中

我告诉你我的代码

Function.php

这是Add Meta Box ::

<?php
function downlaod_meta(){
    add_meta_box('download_id','Download Meta Box','ct_downlaod_meta','post','normal','high');
}
add_action('add_meta_boxes','downlaod_meta');

function ct_downlaod_meta($post){
    $value = get_post_custom($post->ID);

    $url = isset( $value['txt_meta_url'] ) ? esc_attr( $value['txt_meta_url'][0] ) : ''; 
    $title = isset( $value['txt_meta_title'] ) ? esc_attr( $value['txt_meta_title'][0] ) : '';
    $size = isset( $value['txt_meta_size'] ) ? esc_attr( $value['txt_meta_size'][0] ) : '';
    $author = isset( $value['opt_meta_author'] ) ? esc_attr( $value['opt_meta_author'][0] ) : '';
    $editor = isset( $value['txt_meta_editor'] ) ? esc_attr( $value['txt_meta_editor'][0] ) : ''; 

    // Nonce to verify intention later  
    wp_nonce_field( 'save_download_meta', 'download_nonce' ); 

?>
    <table width="100%" border="0" cellspacing="6" cellpadding="0">
        <tr>
            <td>
                <label><strong>Download URL</strong></label>
            </td>
            <td>
                <input type="text" name="txt_meta_url" id="txt_meta_url" size="80" value="<?php echo $url ?>" />
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Download Title</strong></label>
            </td>
            <td>
                <input type="text" name="txt_meta_title" id="txt_meta_title" size="80" value="<?php echo $title ?>" />
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Download Size</strong></label>
            </td>
            <td>
                <input type="text" name="txt_meta_size" id="txt_meta_size" size="80" value="<?php echo $size ?>" />
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Author</strong></label>
            </td>
            <td>
                <select multiple="multiple" name="opt_meta_author" id="opt_meta_author">
                    <option value="Amjad" <?php selected( $author, 'Amjad' ); ?>>Amjad</option>
                </select>
                <br />
        <span style="font-size:12px; font-style:italic; color:#b1b1b1;">Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</span>
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Editor</strong></label>
            </td>
            <td>
                <select name="opt_meta_editor" id="opt_meta_editor" style="width:70%;">
                    <option>:.. Select Editor ..:</option>
            <?php
                $args = array(
                    'orderby' => 'display_name',
                    'order' => 'ASC'
                );

                $user_query = new WP_User_Query($args);

                foreach ( $user_query->results as $user ) {
            ?>
                    <option value="<?php echo $user->display_name ?>" <?php selected( $editor, $user->display_name ); ?>>
                        <?php echo $user->display_name ?>
                    </option>
            <?php
                }
            ?>
                </select>
            </td>
        </tr>
    </table>

<?php
}
?>

这就是元框数据保存的地方

<?php
add_action('save_post','save_download_meta_data');
function save_download_meta_data($post_id){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    if ( !wp_verify_nonce( $_POST['download_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    if ( !isset( $_POST['txt_meta_url'] )){
        update_post_meta( $post_id, 'txt_meta_url', esc_attr( $_POST['txt_meta_url'] ) );
    }
    if ( !isset( $_POST['txt_meta_title'] )){
        update_post_meta( $post_id, 'txt_meta_title', esc_attr( $_POST['txt_meta_title'] ) );
    }
    if ( !isset( $_POST['txt_meta_size'] )){
        update_post_meta( $post_id, 'txt_meta_size', esc_attr( $_POST['txt_meta_size'] ) );
    }
    if ( !isset( $_POST['opt_meta_author'] )){
        update_post_meta( $post_id, 'opt_meta_author', esc_attr( $_POST['opt_meta_author'] ) );
    }
    if ( !isset( $_POST['opt_meta_editor'] )){
        update_post_meta( $post_id, 'opt_meta_editor', esc_attr( $_POST['opt_meta_editor'] ) );
    }
}

&GT;

请任何人帮助我

1 个答案:

答案 0 :(得分:0)

我有解决方案

的functions.php

添加肉盒

<?php

function downlaod_meta($post){
    add_meta_box('download_id','Download Details','ct_downlaod_meta','post','normal','high');
}
add_action('add_meta_boxes','downlaod_meta');

function ct_downlaod_meta($post){

    $txt_meta_url = get_post_meta($post->ID, 'txt_meta_url', true);
    $txt_meta_title = get_post_meta($post->ID, 'txt_meta_title', true);
    $txt_meta_size = get_post_meta($post->ID, 'txt_meta_size', true);
    $opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
    $opt_meta_editor = get_post_meta($post->ID, 'opt_meta_editor', true);

?>
    <table width="100%" border="0" cellspacing="6" cellpadding="0">
        <tr>
            <td>
                <label><strong>Download URL</strong></label>
            </td>
            <td>
                <input type="text" name="txt_meta_url" id="txt_meta_url" size="80" value="<?php echo $txt_meta_url; ?>" />

            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Download Title</strong></label>
            </td>
            <td>
                <input type="text" name="txt_meta_title" id="txt_meta_title" size="80" value="<?php echo $txt_meta_title ?>" />
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Download Size</strong></label>
            </td>
            <td>
                <input type="text" name="txt_meta_size" id="txt_meta_size" size="80" value="<?php echo $txt_meta_size ?>" />
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Author</strong></label>
            </td>
            <td>
                <select multiple="multiple" name="opt_meta_author" id="opt_meta_author">
                <?php
                    $authors = new WP_Query(array( 'post_type' => 'author', 'orderby' => 'title', 'order' => 'ASC' ));

                    while($authors->have_posts()) :
                            $authors->the_post();
                ?>
                    <option value="<?php echo the_ID() ?>" <?php selected( $opt_meta_author, the_ID() ); ?>>
                        <?php echo the_title(); ?>
                    </option>
                <?php
                    endwhile;
                ?>
                </select>
                <br />
        <span style="font-size:12px; font-style:italic; color:#b1b1b1;">Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</span>
            </td>
        </tr>
        <tr>
            <td>
                <label><strong>Editor</strong></label>
            </td>
            <td>
                <select name="opt_meta_editor" id="opt_meta_editor" style="width:70%;">
                    <option>Select Editor</option>
            <?php
                $args = array(
                    'orderby' => 'display_name',
                    'order' => 'ASC'
                );

                $user_query = new WP_User_Query($args);

                foreach ( $user_query->results as $user ) {
            ?>
                    <option value="<?php echo $user->ID ?>" <?php selected( $opt_meta_editor, $user->ID ); ?>>
                        <?php echo $user->display_name ?>
                    </option>
            <?php
                }
            ?>
                </select>
            </td>
        </tr>
    </table>

<?php
}
?>

保存元框数据

<?php
add_action('save_post','save_download_meta_data');
function save_download_meta_data(){
    global $post;

    $txt_meta_url = $_POST['txt_meta_url'];
    $txt_meta_title = $_POST['txt_meta_title'];
    $txt_meta_size = $_POST['txt_meta_size'];
    $opt_meta_editor = $_POST['opt_meta_editor'];

    update_post_meta( $post->ID, 'txt_meta_url', $txt_meta_url);
    update_post_meta( $post->ID, 'txt_meta_title', $txt_meta_title);
    update_post_meta( $post->ID, 'txt_meta_size', $txt_meta_size);
    update_post_meta( $post->ID, 'opt_meta_editor', $opt_meta_editor);
}
?>