Wordpress元数据库与多个字段不保存

时间:2018-02-01 16:25:01

标签: php wordpress custom-wordpress-pages meta-boxes

我有一个名为individual_team_member的自定义页面模板,我想要自定义元数据。元数据显示正确,我的字段显示。问题是当我填写任何字段并点击"更新"在我的帖子上它没有显示元字段中的值。我相信这是由$metaKey造成的,但我不确定为什么价值没有被保存。我已经在下面提供了所有的metaBox代码,提前谢谢!

<?php
/* Create one or more meta boxes to be displayed on the post editor screen. */
function test_wealth_add_meta_boxes( $post ) {

    // Get the page template post meta
    $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
    // If the current page uses our specific
    // template, then output our custom metabox
    if ( 'individual_team_member.php' == $page_template ) {
        add_meta_box(
            'member-title', // Metabox HTML ID attribute
            'Team Member Details', // Metabox title
            'test_page_template_metabox', // callback name
            'page', // post type
            'side', // context (advanced, normal, or side)
            'high' // priority (high, core, default or low)
        );
    }
}
// Make sure to use "_" instead of "-"
add_action( 'add_meta_boxes_page', 'test_wealth_add_meta_boxes' );


/* Display the post meta box. */
function test_page_template_metabox( $post ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'tidehave_wealth_nonce' ); ?>

    <p>
        <label for="member-title"><?php _e("Title"); ?></label>
        <br />
        <input class="" type="text" name="member-title" id="member-title" value="<?php echo get_post_meta( $post->ID, 'title_meta_field', true ); ?>" size="30" />
    </p>
    <p>
        <label for="member-phone"><?php _e("Phone Number"); ?></label>
        <br />
        <input class="" type="phone" name="member-phone" id="member-phone" value="<?php echo get_post_meta( $post->ID, 'test_page_template_metabox2', true ); ?>" size="30" />
    </p>
    <p>
        <label for="member-email"><?php _e("Email"); ?></label>
        <br />
        <input class="" type="text" name="member-email" id="member-email" value="<?php echo get_post_meta( $post->ID, 'test_page_template_metabox3', true ); ?>" size="30" />
    </p>
<?php }

/* Save the meta box's post metadata. */
function test_wealth_save_meta( $post_id, $post ) {

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['tidehave_wealth_nonce'] ) || !wp_verify_nonce( $_POST['tidehave_wealth_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] );
    $phone_meta_value = isset( $_POST['member-phone'] );
    $email_meta_value = isset( $_POST['member-email'] );

    /* Get the meta key. */
    $meta_key1 = 'title_meta_field';
    $meta_key2 = 'test_page_template_metabox2';
    $meta_key3 = 'test_page_template_metabox3';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key1, true );
    $meta_value = get_post_meta( $post_id, $meta_key2, true );
    $meta_value = get_post_meta( $post_id, $meta_key3, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $title_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key1, $title_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $title_meta_value && $title_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key1, $title_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $title_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key1, $meta_value );

    /* If a new meta value was added and there was no previous value, add it. */
    // if ( $phone_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key2, $phone_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $phone_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key2, $phone_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $phone_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key2, $meta_value );

    // /* If a new meta value was added and there was no previous value, add it. */
    // if ( $email_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key3, $email_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $new_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key3, $email_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $email_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key3, $meta_value );
}

/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'test_wealth_save_meta', 10, 3 );

1 个答案:

答案 0 :(得分:1)

替换它:

https://geo.yahoo.com/b?s=792600534

..用这个:

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] );
    $phone_meta_value = isset( $_POST['member-phone'] );
    $email_meta_value = isset( $_POST['member-email'] );

你实际上可以缩短它:

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] ) ? $_POST['member-title'] : '';
    $phone_meta_value = isset( $_POST['member-phone'] ) ? $_POST['member-phone'] : '';
    $email_meta_value = isset( $_POST['member-email'] ) ? $_POST['member-email'] : '';

..进入这个:

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] );
    $phone_meta_value = isset( $_POST['member-phone'] );
    $email_meta_value = isset( $_POST['member-email'] );

    /* Get the meta key. */
    $meta_key1 = 'title_meta_field';
    $meta_key2 = 'test_page_template_metabox2';
    $meta_key3 = 'test_page_template_metabox3';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key1, true );
    $meta_value = get_post_meta( $post_id, $meta_key2, true );
    $meta_value = get_post_meta( $post_id, $meta_key3, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $title_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key1, $title_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $title_meta_value && $title_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key1, $title_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $title_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key1, $meta_value );

    /* If a new meta value was added and there was no previous value, add it. */
    // if ( $phone_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key2, $phone_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $phone_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key2, $phone_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $phone_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key2, $meta_value );

    // /* If a new meta value was added and there was no previous value, add it. */
    // if ( $email_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key3, $email_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $new_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key3, $email_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $email_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key3, $meta_value );