使用GET_POST_CUSTOM从数组中的一个键回显2个值

时间:2011-12-15 作者:John Bentwin

Edit 12/15/2011 Added Radio Button print code

  // Add meta boxes to admin panel only needs to be added once
  add_action(\'admin_menu\', \'plib_add_box\');

  //Add meta boxes to post types
      function plib_add_box() {
          global $meta_box;

          foreach($meta_box as $post_type => $value) {
              add_meta_box($value[\'id\'], $value[\'title\'], \'plib_format_box\', $post_type, $value[\'context\'], $value[\'priority\']);
          }
      }

  //Format meta boxes
      function plib_format_box() {
        global $meta_box, $post;

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

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

  foreach ($meta_box[$post->post_type][\'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[\'default\']) . \'" size="30" style="width:30%" />\'. \' - \'. $field[\'desc\'];
                    break;
                case \'textarea\':
                    echo \'<textarea name="\'. $field[\'id\']. \'" id="\'. $field[\'id\']. \'" cols="60" rows="4" style="width:97%">\'. ($meta ? $meta : $field[\'default\']) . \'</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\'] . \'" id="\' . $field[\'id\'] . \'"\' . ( $meta ? \' checked="checked"\' : \'\' ) . \' />\';
                    break;
            }
            echo     \'<td>\'.\'</tr>\';
        }

        echo \'</table>\';

      }

  // Save data from meta box
      function plib_save_data($post_id) {
          global $meta_box,  $post;

          //Verify nonce
          if (!wp_verify_nonce($_POST[\'plib_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;
          }

          foreach ($meta_box[$post->post_type][\'fields\'] as $field) {
              $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);
              }
          }
      }

  add_action(\'save_post\', \'plib_save_data\');

End Edit on 12/15/2011

Edit #2 Showing Printr output

Array ( 
[_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1323951531:1 )     
[start_date] => Array ( [0] => 2011/11/18 ) [end_date] => Array ( [0] => 2011/12/31 ) 
[opening_time] => Array ( [0] => 2011-11-21 18:00 ) 
[artist_talk_time] => Array ( [0] => 2011-11-22 17:00 ) 
[_thumbnail_id] => Array ( [0] => 130 ) 
[_start_month] => Array ( [0] => 01 ) 
[_start_day] => Array ( [0] => 77 ) 
[_start_hour] => Array ( [0] => 03 ) 
[_start_year] => Array ( [0] => 2011 ) 
[_start_minute] => Array ( [0] => 00 ) 
[_start_eventtimestamp] => Array ( [0] => 201101770300 ) 
[_end_month] => Array ( [0] => 12 ) 
[_end_day] => Array ( [0] => 10 ) 
[_end_hour] => Array ( [0] => 03 ) 
[_end_year] => Array ( [0] => 2011 ) 
[_end_minute] => Array ( [0] => 00 ) 
[_end_eventtimestamp] => Array ( [0] => 201112100300 ) 
[event_venue] => Array ( [0] => Pine Gallery ) 
[closing_time] => Array ( [0] => 2011-12-22 19:00 ) 
[lecture_time] => Array ( [0] => 2011-11-21 19:00 ) 
[panel_time] => Array ( [0] => 2011-11-24 14:00 ) 
[special_event_time] => Array ( [0] => 2011-11-28 19:00 ) 
[workshop_time] => Array ( [0] => 2011-11-28 13:00 ) ) 

End Edit #2

根据这个网站上的建议,我改变了get_post_metaget_post_custom 以减少查询数量。现在,我很难从单选按钮设置的数组中获取两个值。

目标是通过单击单选按钮来设置场馆,并让它设置在阵列中预设的场馆url。我希望能够echo 两个值。

这是我正在使用的代码。

       <?php
       /* creating meta boxes in functions.php */
       $meta_box[\'event\'] = array(
          \'id\' => \'event-meta-details\',
          \'title\' => \'Event Information\',
          \'context\' => \'normal\',
          \'priority\' => \'high\',
          \'fields\' => array(                  
              array(
                  \'name\' => \'Workshop\',
                  \'desc\' => \'YYYY-MM-DD 00:00 24 hour clock\',
                  \'id\' => \'workshop_time\',
                  \'type\' => \'text\',
                  \'default\' => \'\'
              ),
              array(
              \'name\' => \'Venue\',
              \'desc\' => \'Venue of Event\',
              \'id\' => $prefix . \'event_venue\',
              \'type\' => \'radio\',
              \'options\' => array(   
              array(\'name\' => \'Pine Gallery\', \'value\' => \'Pine Gallery\', \'http://www.pinegallery.com\' ),
              array(\'name\' => \'Spruce Gallery\', \'value\' => \'Spruce  Gallery\'),
              array(\'name\' => \'Oak Gallery\', \'value\' => \'Oak Gallery\')
              )
              ),
              array(
                  \'name\' => \'Custom Venue\',
                  \'desc\' => \'Enter Venue Name if Enter Text Below is Checked\',
                  \'id\' => \'custom_event_venue\',
                  \'type\' => \'text\',
                  \'default\' => \'\'
              ),
              array(
                  \'name\' => \'Featured / Reccomended\',
                  \'desc\' => \'Recommended Event\',
                  \'id\' => \'featured_event\',
                  \'type\' => \'checkbox\',
                  \'default\' => \'\'
                )

          )
      );

      /* In the post trying to get both keys in the array*/

      $event_custom_meta=get_post_custom($post->ID); // Get all the data 

      $event_event_venue = $event_custom_meta[\'event_venue\'][0];
      $event_url = $event_custom_meta[\'event_venue\'][1];

      echo $event_event_venue;
      echo $event_url;
      ?>
谢谢你。

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

代码的主要问题是,您只存储值,而不存储场馆名称。一个简单的解决方法是将您的vlaue更改为保存场馆名称,例如:

array(
    \'name\' => \'Venue\',
    \'desc\' => \'Venue of Event\',
    \'id\' => $prefix . \'event_venue\',
    \'type\' => \'radio\',
    \'options\' => array(   
        array(\'name\' => \'Pine Gallery\', \'value\' => \'Pine Gallery***http://www.pinegallery.com\' ),
        array(\'name\' => \'Spruce Gallery\', \'value\' => \'Spruce  Gallery***http://www.sprucegallery.com\'),
        array(\'name\' => \'Oak Gallery\', \'value\' => \'Oak Gallery***http://www.oakgallery.com\')
    )
)
代码的另一个问题是$event_custom_meta[\'event_venue\'] 并且没有名为event_venue 因为您使用$prifix 应该是这样的$event_custom_meta[$prifix.\'event_venue\']

所以现在$event_custom_meta[$prifix.\'event_venue\'][0] 应该只有名称和URL以分隔***因此,将其拆分为一个数组,您将得到位值,如下所示:

$event_venue = $pieces = explode("***", $event_custom_meta[$prifix.\'event_venue\'][0]);
echo \'name: \'.$event_venue[0];
echo \'<br/>url: \'.$event_venue[1];
希望这有帮助

SO网友:jocken

看起来您调用了错误的字段。

尝试print_r($event_custom_meta); 查看正确的。

结束

相关推荐

从Metabox保存数据不起作用

我已经使用了添加metabox的教程。但我的数据不会保存在框中。我在编辑链接页面中有元框,但当我将任何数据放入框中并按下更新按钮时,它不会保存数据。那么实际上,我想知道我什么时候把数据放进框中,我可以使用$\\u POST[]获取数据吗?如果是,请帮助我,使我的代码部分出错。。// backwards compatible add_action( \'admin_init\', \'blc_add_custom_link_box\', 1 ); /*