正确显示Meta-Box数据

时间:2011-06-22 作者:mmaximalist

又快又脏,我有关于音乐会信息的元框。(即地点和地点)并正在设法解决如何properly display 他们在我的岗位上。

现在要显示我拥有的元数据框数据

<?php 
$venue_info = get_post_custom_values("venue_info");
    if (isset($venue_info[0])) {
        };
?>
<?php if( $venue_info[0] ) : ?>
    <?php echo $venue_info[0] ?>
<?php endif; ?>
我意识到这是一个非常jerry rig的方式来显示数据。这就是为什么我希望学习正确的格式。感谢您抽出时间。

这就是我的职能。php看起来像是为了满足您的需要

<?php   
include(\'preset-library.php\');

//I create an array called $meta_box and set the array key to the relevant post type
// If custom post type, change the \'post\' variable, which I don\'t 
$meta_box[\'post\'] = array(

    //This is the id applied to the meta box
    \'id\' => \'post-format-meta\',  

    //This is the title that appears on the meta box container
    \'title\' => \'Additional Post Format Meta\',    

    //This defines the part of the page where the edit screen section should be shown
    \'context\' => \'normal\',    

    //This sets the priority within the context where the boxes should show
    \'priority\' => \'high\',

    //Here we define all the fields we want in the meta box
    \'fields\' => array(
        array(
            \'name\' => \'Venue\',
            \'desc\' => \'venue information\',
            \'id\' => \'venue_info\',
            \'type\' => \'text\',
            \'default\' => \'\'
        ),
        array(
            \'name\' => \'Location\',
            \'desc\' => \'Location of the Venue\',
            \'id\' => \'location_info\',
            \'type\' => \'text\',
            \'default\' => \'\'
        )
    )
);
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\']);
    }
}
//Formatting
function plib_format_box() {
  global $meta_box, $post;

  // 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: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[\'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
    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\');
?>

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

要检查元键值,然后显示:

if ( get_post_meta( $post->ID, \'venue_info\', true ) ) : echo get_post_meta( $post->ID, \'venue_info\', true )

endif;
通过:The Codex

SO网友:Milo

我看到您将地点和位置都存储在“venue\\u info”键下,您如何区分这两者,还是这并不重要?get_post_custom_values() 返回一个数组,因为它的预期用途是在需要多个数组时使用venue_info 与特定帖子关联的密钥。如果只需要一个值,请使用get_post_meta(\'venue_info\').

结束

相关推荐

update = remove code

我在3.1中更新了我的wordpress(我使用的是二十个十),我的一些代码被删除了,我想了解一下:是不是因为我使用的是“二十个主题”,才用新版本替换了“代码”?如果我使用自己的主题,“新版本”是否也会删除代码?所以:要保证代码安全,“plugin”文件夹是唯一的安全区域?下次我更新wordpress版本时,我很有信心。谢谢