自定义帖子类型不会使用我的自定义字段

时间:2014-01-09 作者:MartijnICU

我编写了以下插件:

    <?php

/* Set up the post types */
add_action( \'init\', \'mdg_kjdz_register_post_types\' );

/* Register the post types */
function mdg_kjdz_register_post_types() {

    /* Set up the arguments for the kjdz_adress post type */
    $adress_args = array(
            \'public\' => true,
            \'query_var\' => \'kjdz_adress\',
            \'rewrite\' => array(
                    \'slug\' => \'adres\',
                    \'with_front\' => false,
            ),
            \'supports\' => array(
                    \'title\',
                    \'thumbnail\',
                    \'editor\'
            ),
            \'labels\' => array(
                    \'name\' => \'Locaties\',
                    \'singular\' => \'Locatie\',
                    \'add_new\' => \'Voeg locatie toe\',
                    \'add_new_item\' => \'Voeg locatie toe\',
                    \'edit_item\' => \'Locatie aanpassen\',
                    \'new_item\' => \'Nieuwe locatie\',
                    \'view_item\' => \'Bekijk locatie\',
                    \'search_items\' => \'Doorzoek locaties\',
                    \'not_found\' => \'Geen locaties gevonden\',
                    \'not_found_in_trash\' => \'Geen locaties in prullenbak\'
            ),
    );

    /* Set up custom meta box kjdz_adress post type */
    $kjdz_adress_meta_box = array(
            \'id\' => \'adress-meta-box\',
            \'title\' => \'Gegevens locatie\',
            \'page\' => \'kjdz_adress\',
            \'context\' => \'normal\',
            \'priority\' => \'high\',
            \'fields\'=> array(
                    array(
                            \'name\' => \'Adres\',
                            \'desc\' => \'Adres locatie\',
                            \'id\' => \'adress\',
                            \'type\' => \'text\',
                            \'std\' => \'Voer straatnaam en huisnummer in.\'
                    ),
                    array(
                            \'name\' => \'Plaatsnaam\',
                            \'desc\' => \'Plaatsnaam locatie\',
                            \'id\' => \'town\',
                            \'type\' => \'text\',
                            \'std\' => \'Voer de plaatsnaam in.\'
                    ),
                    array(
                            \'name\' => \'Soort locatie\',
                            \'id\' => \'location_type\',
                            \'type\' => \'radio\',
                            \'options\' => array(
                                    array(\'name\' => \'hoofdvestiging\', \'value\' => \'Hoofdvestiging\'),
                                    array(\'name\' => \'nevenvestiging\', \'value\' => \'Nevenvestiging\')
                            )
                    ),
                    array(
                            \'name\' => \'Openingstijden\',
                            \'id\' => \'opening_days1\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'id\' => \'opening_days2\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'id\' => \'opening_days3\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'id\' => \'opening_days4\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'id\' => \'opening_days5\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'id\' => \'opening_days6\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'id\' => \'opening_days7\',
                            \'type\' => \'select\',
                            \'options\' => array(\'Maandag\', \'Dinsdag\', \'Woensdag\', \'Donderdag\', \'Vrijdag\', \'Zaterdag\', \'Zondag\')
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours1_1\',
                            \'type\' => \'text\',
                            \'std\' => \'09:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours1_2\',
                            \'type\' => \'text\',
                            \'std\' => \'18:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours2_1\',
                            \'type\' => \'text\',
                            \'std\' => \'09:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours2_2\',
                            \'type\' => \'text\',
                            \'std\' => \'18:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours3_1\',
                            \'type\' => \'text\',
                            \'std\' => \'09:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours3_2\',
                            \'type\' => \'text\',
                            \'std\' => \'18:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours4_1\',
                            \'type\' => \'text\',
                            \'std\' => \'09:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours4_2\',
                            \'type\' => \'text\',
                            \'std\' => \'18:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours5_1\',
                            \'type\' => \'text\',
                            \'std\' => \'09:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours5_2\',
                            \'type\' => \'text\',
                            \'std\' => \'18:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours6_1\',
                            \'type\' => \'text\',
                            \'std\' => \'09:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours6_2\',
                            \'type\' => \'text\',
                            \'std\' => \'18:00\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de openingstijd in (vb. 09:00)\',
                            \'id\' => \'opening_hours7_1\',
                            \'type\' => \'text\',
                            \'std\' => \'\'
                    ),
                    array(
                            \'name\' => \'\',
                            \'desc\' => \'Voer de sluitingstijd in (vb. 18:00)\',
                            \'id\' => \'opening_hours7_2\',
                            \'type\' => \'text\',
                            \'std\' => \'\'
                    )
                )
            );

            /* Register the post types */
            register_post_type( \'kjdz_adress\', $adress_args );
}

add_action(\'admin_menu\', \'kjdz_adress_add_box\');

/* Add meta box */
function kjdz_adress_add_box() {
        global $kjdz_adress_meta_box;

        add_meta_box($kjdz_adress_meta_box[\'id\'], $kjdz_adress_meta_box[\'title\'], \'kjdz_adress_show_box\', $kjdz_adress_meta_box[\'page\'], $kjdz_adress_meta_box[\'content\'], $kjdz_adress_meta_box[\'priority\']);
}

/* Callback function to show fields in meta box */
function kjdz_adress_show_box() {
    global $kjdz_adress_meta_box, $post;

    /* Use nonce for verification */
    echo \'<input type="hidden" name="kjdz_adress_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)) ,\'" />\';

    echo \'<table class="form-table">\';

    foreach ($kjdz_adress_meta_box[\'fields\'] as $field) {
            /* Get current post meta data   */
            $meta = get_post_meta($post->ID, $field[\'id\'], true);

            echo \'<tr>\',
                            \'<th style="width: 20%;"><label for="\', $field[\'id\'], \'">\', $field[\'name\'], \'</label></th>\',
                            \'<td>\';
            switch ($field[\'type\']) {
                    case \'text\':
                            echo \'<input type="text" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" value="\', $meta ? $meta : $field[\'std\'], \'" size="30" style="width: 97%" />\', \'<br />\', $field[\'desc\'];
                            break;
                    case \'textarea\':
                            echo \'<textarea name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" cols="60" rows="4" style="width: 97%">\', $meta ? $meta : $field[\'std\'], \'</textarea>\', \'<br />\', $field[\'desc\'];
                            break;
                    case \'select\':
                            echo \'<select name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'">\';
                            foreach ($field[\'options\'] as $option) {
                                    echo \'<option \', $meta == $option ? \' selected="selected"\' : \'\', \'>\', $option, \'</option>\';
                            }
                            echo \'</select>\';
                            break;
                    case \'radio\':
                            foreach ($field[\'options\'] as $option) {
                                    echo \'<input type="radio" name="\', $field[\'id\'], \'" value="\', $option[\'value\'], \'"\', $meta == $option[\'value\'] ? \' checked="checked"\' : \'\', \'/>\', $option[\'name\'];
                            }
                            break;
                    case \'checkbox\':
                            echo \'<input type="checkbox" name="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \'/>\';
                            break;
            }
            echo        \'</td><td>\',
                        \'</td></tr>\';
    }

    echo \'</table>\';
}               

?>
在管理菜单中显示为自定义帖子类型“kjdz\\u address”或Locatie。

但我的自定义字段不会显示。我错过了什么?

1 个回复
SO网友:GhostToast

您正在定义$kjdz_adress_meta_box 在一个函数中,并在另一个函数中引用它。只要将它保留在您试图使用它的函数中,这可能会修复它。如果在多个函数中需要它,也可以在其他地方定义它。我不认为$global 调用必然会超越函数。

结束

相关推荐

将内容添加到/wp-admin/plugin-install.php管理屏幕

我想向上显示的插件信息添加一些外部内容/wp-admin/plugin-install.php 管理屏幕。/wp-admin/plugin-install.php 调用以下命令:$wp_list_table = _get_list_table(\'WP_Plugin_Install_List_Table\'); 如果我破解这个核心文件作为测试,我可以添加内容,我想,好的。然而,作为一名新的WP开发者,我真的很难看到如何通过插件添加这些内容。class WP_Plugin_Install_List