在编辑帖子中显示META_VALUE数组

时间:2012-07-01 作者:maisdesign

我想做的是添加一个新的列来编辑文章页面(完成),并在该列中显示从一组自定义字段中获取的数据(部分完成)。

这是我的代码(我将其用作插件,以便更简单地修改它)。它在帖子中添加了元框,因此我可以轻松添加自定义字段。

<?php
// the plugin base directory
global $maismeta_base_dir;
$maismeta_base_dir = plugin_dir_url(__FILE__);

// Add the Meta Box
function add_custom_meta_box() {
    add_meta_box(
        \'custom_meta_box\', // $id
        \'Impostazioni di ricerca\', // $title 
        \'show_custom_meta_box\', // $callback
        \'post\', // $page
        \'normal\', // $context
        \'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_custom_meta_box\');

// Field Array
$prefix = \'custom_\';
$custom_meta_fields = array(
    array (
        \'label\' => __(\'Proprietario\',\'maismeta\'),
        \'desc\'  => __(\'Inserisci le informazioni sul proprietario.\',\'maismeta\'),
        \'id\'    => $prefix.\'textarea_proprietario\',
        \'type\'  => \'textarea\',
    ),
    array (
        \'label\' => __(\'Sesso affittuario\',\'maismeta\'),
        \'desc\'  => __(\'Inserisci il sesso disponibile.\',\'maismeta\'),
        \'id\'    => $prefix.\'checkbox_group_sesso\',
        \'type\'  => \'checkbox_group\',
        \'options\' => array (
            \'Uomo\' => array (
                \'label\' => __(\'Uomo\',\'maismeta\'),
                \'value\' => \'Uomo\'
            ),
            \'Donna\' => array (
                \'label\' => __(\'Donna\',\'maismeta\'),
                \'value\' => \'Donna\'
            ),
            \'Altro\' => array (
                \'label\' => __(\'Altro\',\'maismeta\'),
                \'value\' => \'Altro\'
            )
        )
    ),
    array (
        \'label\' => __(\'Numero occupanti\',\'maismeta\'),
        \'desc\'  => __(\'Inserisci il numero degli occupanti.\',\'maismeta\'),
        \'id\'    => $prefix.\'checkbox_group_occupanti\',
        \'type\'  => \'checkbox_group\',
        \'options\' => array (
            \'1\' => array (
                \'label\' => __(\'1\',\'maismeta\'),
                \'value\' => \'1\'
            ),
            \'2\' => array (
                \'label\' => __(\'2\',\'maismeta\'),
                \'value\' => \'2\'
            ),
            \'3\' => array (
                \'label\' => __(\'3\',\'maismeta\'),
                \'value\' => \'3\'
            ),
            \'4\' => array (
                \'label\' => __(\'4\',\'maismeta\'),
                \'value\' => \'4\'
            ),
            \'5\' => array (
                \'label\' => __(\'5\',\'maismeta\'),
                \'value\' => \'5\'
            ),
            \'6\' => array (
                \'label\' => __(\'6\',\'maismeta\'),
                \'value\' => \'6\'
            ),
            \'7\' => array (
                \'label\' => __(\'7\',\'maismeta\'),
                \'value\' => \'7\'
            ),
            \'8\' => array (
                \'label\' => __(\'8\',\'maismeta\'),
                \'value\' => \'8\'
            ),
        )
    ),
    array (
        \'label\' => __(\'Facolt&agrave; di riferimento\',\'maismeta\'),
        \'desc\'  => __(\'Scegli le facolt&agrave; di riferimento.\',\'maismeta\'),
        \'id\'    => $prefix.\'checkbox_group_facolta\',
        \'type\'  => \'checkbox_group\',
        \'options\' => array (
            \'Agraria\' => array (
                \'label\' => __(\'Agraria\',\'maismeta\'),
                \'value\' => \'Agraria\'
            ),
            \'Economia\' => array (
                \'label\' => __(\'Economia\',\'maismeta\'),
                \'value\' => \'Economia\'
            ),
            \'Farmacia\' => array (
                \'label\' => __(\'Farmacia\',\'maismeta\'),
                \'value\' => \'Farmacia\'
            ),
            \'Giurisprudenza\' => array (
                \'label\' => __(\'Giurisprudenza\',\'maismeta\'),
                \'value\' => \'Giurisprudenza\'
            ),
            \'Ingegneriea\' => array (
                \'label\' => __(\'Ingegneriea\',\'maismeta\'),
                \'value\' => \'Ingegneriea\'
            ),
            \'Lettere e Filosofia\' => array (
                \'label\' => __(\'Lettere e Filosofia\',\'maismeta\'),
                \'value\' => \'Lettere e Filosofia\'
            ),
            \'Lingue e letteratura straniera\' => array (
                \'label\' => __(\'Lingue e letteratura straniera\',\'maismeta\'),
                \'value\' => \'Lingue e letteratura straniera\'
            ),
            \'Medicina e chirurgia\' => array (
                \'label\' => __(\'Medicina e chirurgia\',\'maismeta\'),
                \'value\' => \'Medicina e chirurgia\'
            ),
            \'Scienze della formazione\' => array (
                \'label\' => __(\'Scienze della formazione\',\'maismeta\'),
                \'value\' => \'Scienze della formazione\'
            ),
            \'Scienze matematiche fisiche e naturali\' => array (
                \'label\' => __(\'Scienze matematiche fisiche e naturali\',\'maismeta\'),
                \'value\' => \'Scienze matematiche fisiche e naturali\'
            ),
            \'Scienze politiche\' => array (
                \'label\' => __(\'Scienze politiche\',\'maismeta\'),
                \'value\' => \'Scienze politiche\'
            )
        )
    ),

);

/* jQuery era qui */

// The Callback
function show_custom_meta_box() {
    global $custom_meta_fields, $post;
    // Use nonce for verification
    echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

    // Begin the field table and loop
    echo \'<table class="form-table">\';
    foreach ($custom_meta_fields as $field) {
        // get value of this field if it exists for this post
        $meta = get_post_meta($post->ID, $field[\'id\'], true);
        // begin a table row with
        echo \'<tr>
                <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                <td>\';
                switch($field[\'type\']) {
                    // text
                    case \'text\':
                        echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
                                <br /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // textarea
                    case \'textarea\':
                        echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea>
                                <br /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // checkbox
                    case \'checkbox\':
                        echo \'<input type="checkbox" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" \',$meta ? \' checked="checked"\' : \'\',\'/>
                                <label for="\'.$field[\'id\'].\'">\'.$field[\'desc\'].\'</label>\';
                    break;
                    // select
                    case \'select\':
                        echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">\';
                        foreach ($field[\'options\'] as $option) {
                            echo \'<option\', $meta == $option[\'value\'] ? \' selected="selected"\' : \'\', \' value="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</option>\';
                        }
                        echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // radio
                    case \'radio\':
                        foreach ( $field[\'options\'] as $option ) {
                            echo \'<input type="radio" name="\'.$field[\'id\'].\'" id="\'.$option[\'value\'].\'" value="\'.$option[\'value\'].\'" \',$meta == $option[\'value\'] ? \' checked="checked"\' : \'\',\' />
                                    <label for="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</label><br />\';
                        }
                        echo \'<span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // checkbox_group
                    case \'checkbox_group\':
                        foreach ($field[\'options\'] as $option) {
                            echo \'<input type="checkbox" value="\'.$option[\'value\'].\'" name="\'.$field[\'id\'].\'[]" id="\'.$option[\'value\'].\'"\',$meta && in_array($option[\'value\'], $meta) ? \' checked="checked"\' : \'\',\' /> 
                                    <label for="\'.$option[\'value\'].\'">\'.$option[\'label\'].\'</label><br />\';
                        }
                        echo \'<span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // tax_select
                    case \'tax_select\':
                        echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">
                                <option value="">Select One</option>\'; // Select One
                        $terms = get_terms($field[\'id\'], \'get=all\');
                        $selected = wp_get_object_terms($post->ID, $field[\'id\']);
                        foreach ($terms as $term) {
                            if (!empty($selected) && !strcmp($term->slug, $selected[0]->slug)) 
                                echo \'<option value="\'.$term->slug.\'" selected="selected">\'.$term->name.\'</option>\'; 
                            else
                                echo \'<option value="\'.$term->slug.\'">\'.$term->name.\'</option>\'; 
                        }
                        $taxonomy = get_taxonomy($field[\'id\']);
                        echo \'</select><br /><span class="description"><a href="\'.get_bloginfo(\'home\').\'/wp-admin/edit-tags.php?taxonomy=\'.$field[\'id\'].\'">Manage \'.$taxonomy->label.\'</a></span>\';
                    break;
                    // post_list
                    case \'post_list\':
                    $items = get_posts( array (
                        \'post_type\' => $field[\'post_type\'],
                        \'posts_per_page\' => -1
                    ));
                        echo \'<select name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'">
                                <option value="">Select One</option>\'; // Select One
                            foreach($items as $item) {
                                echo \'<option value="\'.$item->ID.\'"\',$meta == $item->ID ? \' selected="selected"\' : \'\',\'>\'.$item->post_type.\': \'.$item->post_title.\'</option>\';
                            } // end foreach
                        echo \'</select><br /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // date
                    case \'date\':
                        echo \'<input type="text" class="datepicker" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" />
                                <br /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // slider
                    case \'slider\':
                    $value = $meta != \'\' ? $meta : \'0\';
                        echo \'<div id="\'.$field[\'id\'].\'-slider"></div>
                                <input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$value.\'" size="5" />
                                <br /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // image
                    global $secondazona;$secondazona = plugin_dir_url(__FILE__);
                    case \'image\':
                        $zonaimmi = $secondazona.\'images/image.png\';    
                        echo \'<span class="custom_default_image" style="display:none">\'.$zonaimmi.\'</span>\';
                        if ($meta) { $zonaimmi = wp_get_attachment_image_src($meta, \'medium\');  $zonaimmi = $zonaimmi[0]; }             
                        echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" />
                                    <img src="\'.$zonaimmi.\'" class="custom_preview_image" alt="" /><br />
                                        <input class="custom_upload_image_button button" type="button" value="Choose Image" />
                                        <small>&nbsp;<a href="#" class="custom_clear_image_button">Remove Image</a></small>
                                        <br clear="all" /><span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                    // repeatable
                    case \'repeatable\':
                        echo \'<a class="repeatable-add button" href="#">+</a>
                                <ul id="\'.$field[\'id\'].\'-repeatable" class="custom_repeatable">\';
                        $i = 0;
                        if ($meta) {
                            foreach($meta as $row) {
                                echo \'<li><span class="sort hndle">|||</span>
                                            <input type="text" name="\'.$field[\'id\'].\'[\'.$i.\']" id="\'.$field[\'id\'].\'" value="\'.$row.\'" size="30" />
                                            <a class="repeatable-remove button" href="#">-</a></li>\';
                                $i++;
                            }
                        } else {
                            echo \'<li><span class="sort hndle">|||</span>
                                        <input type="text" name="\'.$field[\'id\'].\'[\'.$i.\']" id="\'.$field[\'id\'].\'" value="" size="30" />
                                        <a class="repeatable-remove button" href="#">-</a></li>\';
                        }
                        echo \'</ul>
                            <span class="description">\'.$field[\'desc\'].\'</span>\';
                    break;
                } //end switch
        echo \'</td></tr>\';
    } // end foreach
    echo \'</table>\'; // end table
}

function remove_taxonomy_boxes() {
    remove_meta_box(\'categorydiv\', \'post\', \'side\');
}
add_action( \'admin_menu\' , \'remove_taxonomy_boxes\' );

// Save the Data
function save_custom_meta($post_id) {
    global $custom_meta_fields;

    // verify nonce
    if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__))) 
        return $post_id;
    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
        return $post_id;
    // check permissions
    if (\'page\' == $_POST[\'post_type\']) {
        if (!current_user_can(\'edit_page\', $post_id))
            return $post_id;
        } elseif (!current_user_can(\'edit_post\', $post_id)) {
            return $post_id;
    }

    // loop through fields and save the data
    foreach ($custom_meta_fields as $field) {
        if($field[\'type\'] == \'tax_select\') continue;
        $old = get_post_meta($post_id, $field[\'id\'], true);
        $new = $_POST[$field[\'id\']];
        if ($new && $new != $old) {
            update_post_meta($post_id, $field[\'id\'], $new);
        } elseif (\'\' == $new && $old) {
            delete_post_meta($post_id, $field[\'id\'], $old);
        }
    } // enf foreach

    // save taxonomies
    $post = get_post($post_id);
    $category = $_POST[\'category\'];
    wp_set_object_terms( $post_id, $category, \'category\' );
}
add_action(\'save_post\', \'save_custom_meta\');


/* nuova posizione */
// enqueue scripts and styles, but only if is_admin

?>
如您所见,我有两组复选框,第一组有3个可能的值:

\'Uomo\',\'Donna\',\'Altro\'因此,用户可以轻松地选择1、2、3或所有值。

然后在我的编辑贴子页面中显示另外两个列:

// ALL POST TYPES: posts AND custom post types
add_filter(\'manage_posts_columns\', \'colonne_informative_head\');
add_action(\'manage_posts_custom_column\', \'colonna_proprietario\', 10, 2);


// ADD NEW COLUMN
function colonne_informative_head($defaults) {
    $defaults[\'custom_textarea_proprietario\'] = \'Proprietario\';
    $defaults[\'custom_checkbox_group_sesso\'] = \'Sesso occupanti\';
    return $defaults;
};

// SHOW THE FEATURED IMAGE
function colonna_proprietario($column_name, $post_ID) {
    if ($column_name == \'custom_textarea_proprietario\') {
        $post_colonna_proprietario = get_post_meta($post_ID, \'custom_textarea_proprietario\', true);
        if ($post_colonna_proprietario) {
            echo $post_colonna_proprietario;
        }
    }; 

    if ($column_name == \'custom_checkbox_group_sesso\') {
        $sesso_occupanti = get_post_meta($post_ID, "custom_checkbox_group_sesso", true);
      //check that we have a custom field
      if ($sesso_occupanti != "")
      {
        // Separate our comma separated list into an array
        $sesso_occupanti = explode(",", $sesso_occupanti);
        //loop through our new array
        foreach ($sesso_occupanti as $sesso)
        {
          echo $sesso ;
        }
      }
        }; 
    };
我对名为:custom_textarea_proprietario 因为只回显1个值相对容易。

但我不知道如何呼应第二个冒号的值:custom_checkbox_group_sesso 事实上,它只是回声Array\' 没有别的了。如果打开Php my admin并查找我存储的数据,我可以看到:

a:3:{i:0;s:4:"Uomo";i:1;s:5:"Donna";i:2;s:14:"Giurisprudenza";}

在这种情况下,我想在我的专栏中回应的是:

然后再加上一个列:

我几乎是这方面的新手,所以请对我好一点,试着一步一步地解释。

1 个回复
SO网友:maisdesign

错误就在这里:(谢谢@Rarst)

 if ($column_name == \'custom_checkbox_group_sesso\') {
            $sesso_occupanti = get_post_meta($post_ID, "custom_checkbox_group_sesso", true);
          //check that we have a custom field
          if ($sesso_occupanti != "")
          {
            // Separate our comma separated list into an array
            $sesso_occupanti = explode(",", $sesso_occupanti);
            //loop through our new array
            foreach ($sesso_occupanti as $sesso)
            {
              echo $sesso ;
            }
          }
            }; 
我所要做的就是删除这句话:

$sesso_occupanti = explode(",", $sesso_occupanti);
因为,作为“稀有”said:

若您正在存储阵列,那个么您也可以返回阵列,并且不需要对其进行分解。

结束

相关推荐

无法使用wp_EDITOR()从WPAlChemy metabox获取<p>标记

我正在一个项目中使用WPAlchemy。对于一个模板,我需要使用一个自定义的metabox,它可以很好地与tinyMCE编辑器配合使用。在模板上插入元框的代码(这是https://github.com/helgatheviking/WP-Alchemy-Holy-Grail-Theme/ 因为我在一次Loooog搜索后最终尝试了它,但是macache。<div class=\"my_meta_control metabox\"> <p> <?php $mb-&