自定义帖子类型的WordPress元框

自定义帖子类型的WordPress元框,wordpress,custom-post-type,meta-boxes,Wordpress,Custom Post Type,Meta Boxes,我有一个自定义的帖子类型(“站点”),我正在使用它来显示我们的客户,我正在尝试向添加/编辑站点页面添加一个自定义元框。我使用的是WordPress codex()中的示例,我能够在添加/编辑站点页面中显示元框的标题,但是没有文本框来输入网站URL 以下是我的代码: <?php // Add the Meta Box /* function nw_add_custom_meta_box() { add_meta_box( 'website', // $id

我有一个自定义的帖子类型(“站点”),我正在使用它来显示我们的客户,我正在尝试向添加/编辑站点页面添加一个自定义元框。我使用的是WordPress codex()中的示例,我能够在添加/编辑站点页面中显示元框的标题,但是没有文本框来输入网站URL

以下是我的代码:

<?php

// Add the Meta Box
/* function nw_add_custom_meta_box() {
    add_meta_box(
        'website', // $id
        'Website', // $title 
        'show_custom_meta_box', // $callback
        'site', // $page
        'normal', // $context
        'high'); // $priority
}
add_action('add_meta_boxes', 'nw_add_custom_meta_box');
*/


/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function nw_add_custom_meta_box() {

    $screens = array( 'site' ); // add items to add to multiple post types

    foreach ( $screens as $screen ) {
        add_meta_box(
        'website', // $id
        'Website', // $title 
        'show_custom_meta_box', // $callback
        $screen, // $page
        'normal', // $context
        'high' // $priority
        );
    }
}
add_action( 'add_meta_boxes', 'nw_add_custom_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function myplugin_meta_box_callback( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( 'website', 'website_nonce' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, '_website', true );

    echo '<label for="website">';
    _e( 'Website Address', 'myplugin_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="website" name="website" value="' . esc_attr( $value ) . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function nuggetweb_save_meta_box_data( $post_id ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST['website_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['website_nonce'], 'website' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    /* OK, it's safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST['website'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['website'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, '_website', $my_data );
}
add_action( 'save_post', 'nuggetweb_save_meta_box_data' );

?>

除了您没有使用正确的元框回调之外,其他一切都是正确的,请更改您的:

function myplugin_meta_box_callback( $post )
致:

正如您在此处定义的名称:

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function nw_add_custom_meta_box() {

    $screens = array( 'site' ); // add items to add to multiple post types

    foreach ( $screens as $screen ) {
        add_meta_box(
        'website', // $id
        'Website', // $title 
        'show_custom_meta_box', // $callback
        $screen, // $page
        'normal', // $context
        'high' // $priority
        );
    }
}

所有操作都是正确的,只是您没有使用正确的元框回调,请更改您的:

function myplugin_meta_box_callback( $post )
致:

正如您在此处定义的名称:

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function nw_add_custom_meta_box() {

    $screens = array( 'site' ); // add items to add to multiple post types

    foreach ( $screens as $screen ) {
        add_meta_box(
        'website', // $id
        'Website', // $title 
        'show_custom_meta_box', // $callback
        $screen, // $page
        'normal', // $context
        'high' // $priority
        );
    }
}

这里的samplepost表示自定义post类型

<?php
add_action( 'admin_init', 'my_admin_samplepost' );
function my_admin_samplepost() {
    add_meta_box( 'samplepost_meta_box', 'Car Details', 'display_samplepost_meta_box','samplepost', 'normal', 'high' );
}
function display_samplepost_meta_box( $samplepost ) {
    ?>
    <h4>General Details</h4>
    <table width="100%">
        <tr>
            <td style="width: 25%">Monthly Paymeny</td>
            <td><input type="text" style="width:425px;" name="meta[payment]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'payment', true ) );?>" />
            </td>
        </tr>
        <tr>
            <td>Price ($)</td>
            <td><input type="text" style="width:425px;" name="meta[price]" placeholder="$" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'price', true ) );?>" />
            </td>
        </tr>
        <tr>
            <td>Milage</td>
            <td><input type="text" style="width:425px;" name="meta[milage]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'milage', true ) );?>" />
            </td>
        </tr>
    </table>
<?php 
}
add_action( 'save_post', 'add_samplepost_fields', 10, 2 );
function add_samplepost_fields( $samplepost_id, $samplepost ) {
    if ( $samplepost->post_type == 'samplepost' ) {
        if ( isset( $_POST['meta'] ) ) {
            foreach( $_POST['meta'] as $key => $value ){
                update_post_meta( $samplepost_id, $key, $value );
            }
        }
    }
}

一般细节
月薪

这里的samplepost表示自定义post类型

<?php
add_action( 'admin_init', 'my_admin_samplepost' );
function my_admin_samplepost() {
    add_meta_box( 'samplepost_meta_box', 'Car Details', 'display_samplepost_meta_box','samplepost', 'normal', 'high' );
}
function display_samplepost_meta_box( $samplepost ) {
    ?>
    <h4>General Details</h4>
    <table width="100%">
        <tr>
            <td style="width: 25%">Monthly Paymeny</td>
            <td><input type="text" style="width:425px;" name="meta[payment]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'payment', true ) );?>" />
            </td>
        </tr>
        <tr>
            <td>Price ($)</td>
            <td><input type="text" style="width:425px;" name="meta[price]" placeholder="$" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'price', true ) );?>" />
            </td>
        </tr>
        <tr>
            <td>Milage</td>
            <td><input type="text" style="width:425px;" name="meta[milage]" value="<?php echo esc_html( get_post_meta( $samplepost->ID, 'milage', true ) );?>" />
            </td>
        </tr>
    </table>
<?php 
}
add_action( 'save_post', 'add_samplepost_fields', 10, 2 );
function add_samplepost_fields( $samplepost_id, $samplepost ) {
    if ( $samplepost->post_type == 'samplepost' ) {
        if ( isset( $_POST['meta'] ) ) {
            foreach( $_POST['meta'] as $key => $value ){
                update_post_meta( $samplepost_id, $key, $value );
            }
        }
    }
}

一般细节
月薪