自定义发布类型的编辑页面中未显示自定义字段值

时间:2012-07-27 作者:Anoop D

单击“编辑”按钮时,自定义字段值未显示在各个字段中。。当我们创建新帖子时,它们只是显示为空白。我可以在编辑时保存自定义字段中的值。。。但是保存的值仍然没有显示在字段中(这里是“id”文本和“membership”单选按钮),在“编辑”帖子时,id字段显示为空,单选按钮显示默认选中的值

有人能指出我做错了什么吗
以下是代码:

add_action( \'init\', \'codex_custom_init\' );
add_action( \'admin_menu\',\'member_manager_add_meta\' );
add_action( \'save_post\', \'member_manager_save_extras\' );
add_filter( "manage_edit-member_columns", "change_columns" );
add_action( "manage_member_posts_custom_column", "custom_columns", 10, 2 );

function codex_custom_init() {
    $labels = array(
        \'name\' => _x(\'Members\', \'post type general name\'),
        \'singular_name\' => _x(\'Member\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New\', \'Member\'),
        \'add_new_item\' => __(\'Add New Member\'),
        \'edit_item\' => __(\'Edit Member\'),
        \'new_item\' => __(\'New Member\'),
        \'all_items\' => __(\'All Members\'),
        \'view_item\' => __(\'View Member\'),
        \'search_items\' => __(\'Search Members\'),
        \'not_found\' =>  __(\'No members found\'),
        \'not_found_in_trash\' => __(\'No members found in Trash\'), 
        \'parent_item_colon\' => \'\',
        \'menu_name\' => __(\'Members\')
    );
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true, 
        \'show_in_menu\' => true, 
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'has_archive\' => true, 
        \'hierarchical\' => false,
        \'menu_position\' => null,
        \'supports\' => array( \'title\')
    ); 
    register_post_type(\'Member\',$args);
}


function member_manager_add_meta(){
    add_meta_box("members-meta", "Member Details","member_manager_meta_options",\'member\',\'normal\',\'high\');
}


function member_manager_meta_options(){
    global $post;

    $values =get_post_custom($post->ID);
    $id = isset( $values["isprp_id" ]) ? $values["isprp_id" ] : \' \';
    $membership = isset( $values["isprp_member" ]) ? esc_attr(  $values["isprp_member" ]): \'\';

    wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
    ?>
        <div class="member_manager_extras">
        <style type="text/css">
        <?php include(\'member_manager.css\'); ?>
        </style>
            <div><label for="isprp_id">ISPRP ID</label><input type="text" name="isprp_id" id="isprp_id"  value ="<?php echo $id; ?>" /></div>

        <div><label for="isprp_member">Membership Category</label>
        <input type="radio" name="isprp_member"  value="Life Member" <?php selected($membership,\'Life Member\'); ?>checked />Life Member<input type="radio" name="isprp_member"  value="Associate Member" <?php selected($membership,\'Associate Member\'); ?> />Associate  Member</div>
    <?php
 }

 // Save the Data


function member_manager_save_extras($post_id){
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
        return; 

    else if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'my_meta_box_nonce\' ) ) 
        return; 

    else if( !current_user_can( \'edit_post\' ) ) 
        return $post_id; 

    else{
        update_post_meta($post_id, "isprpid",$_POST["isprp_id"]);
        update_post_meta($post_id, "isprpmember",$_POST["isprp_member"]);
    }
}


function change_columns($cols){
    $cols =array(       
        \'cb\' => \'<input type="checkbox" />\',
        \'title\'=>(\'Name\'),
        \'isprpid\' => __(\'ID\',\'trans\'),
        \'isprpmember\' =>__(\'Membership\',\'trans\')        
    );

    return $cols;
}

function custom_columns($column,$post_id){
    switch ($column){
    case \'isprpid\':
        $isprpid = get_post_meta( $post_id, \'isprpid\', true);
       echo \'<a href="\' . $isprpid . \'">\' . $isprpid. \'</a>\';
       break;
    case \'isprpmember\':
        $isprpmember = get_post_meta( $post_id, \'isprpmember\', true);
       echo \'<a href="\' . $isprpmember . \'">\' . $isprpmember. \'</a>\';
       break;
    }
}

1 个回复
SO网友:brasofilo

如果对WordPress功能的功能有疑问,请咨询Codex:
http://codex.wordpress.org/Function_Reference/register_post_type

在那里,我们可以看到,注册帖子时supports 参数有很多选项,其中之一custom-fields.

因此,这将启用CPT的CF框:

$args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true, 
    \'show_in_menu\' => true, 
    \'query_var\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'has_archive\' => true, 
    \'hierarchical\' => false,
    \'menu_position\' => null,
    \'supports\' => array( \'title\', \'custom-fields\' )
); 
此外,还有一个遗漏的结尾</div> 在函数中member_manager_meta_options.

[UPDATE]

我将标记此问题(&;A用于关闭,因为太本地化了。它的问题是post元键名称中的错误和变量值的错误读取。

如果在函数中转储以下调试信息member_manager_meta_options, 您将看到为什么没有读取值(because they are being saved).

$values =get_post_custom($post->ID);
echo \'<pre>\' . print_r( $values,true ) . \'</pre>\';
因此,工作函数是:

function member_manager_meta_options(){
    global $post;  
    $values =get_post_custom($post->ID);
    //echo \'<pre>\' . print_r( $values,true ) . \'</pre>\';
    $id = isset( $values["isprp_id" ]) ? esc_attr($values["isprp_id" ][0]) : \' \';
    $member_life = ( \'Life Member\' == $values["isprp_member" ][0] ) ? \'checked\' : \'\';
    $member_assoc = ( \'Associate Member\' == $values["isprp_member" ][0] ) ? \'checked\' : \'\';
    wp_nonce_field( \'my_meta_box_nonce\', \'meta_box_nonce\' );
    ?>
        <div class="member_manager_extras">
        <style type="text/css">
        <?php include(\'member_manager.css\'); ?>
        </style>
            <div>
                 <label for="isprp_id">ISPRP ID</label>
                 <input type="text" name="isprp_id" id="isprp_id"  value ="<?php echo $id; ?>" />
            </div>
            <div>
                 <label for="isprp_member">Membership Category</label>
                 <input type="radio" name="isprp_member"  value="Life Member" <?php echo $member_life; ?> />Life Member
                 <input type="radio" name="isprp_member"  value="Associate Member" <?php echo $member_assoc; ?> />Associate  Member
            </div>
    <?php
}
以及post_meta 关键名称是isprp_memberisprpmember (和ispr_id 以及)未通过代码进行合并。它们被用作列ID和后置元键。

结束

相关推荐

在wp-admin中更改默认页面菜单视图

我似乎不知道如何更改管理菜单中“页面”的默认视图。初始视图是“页面”视图(/wp-admin/edit.php?post_type=page)它怎么能把这个改变成其他东西呢像“添加新页面”一样?(/wp-admin/post-new.php?post_type=page)我尝试了几种不同的选项,包括用自定义菜单重建菜单,但WordPress似乎会自动重定向“页面”视图(/wp-admin/edit.php?post_type=page).