2017-07-17 62 views
0

我要去建立自己的主题 我尝试添加选择到post_type元框,但每次更新后我选择不选择的选项,但显示的第一个选项(空白值)保存在元框中选定选项

这里是我的代码

function mhs_data() { 
     global $post; 
     echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
     wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 

     $nisn = get_post_meta($post->ID, '_nisn', true); 
     $rel = get_post_meta($post->ID, '_rel', true); 
    } 

      echo '<p>NISN</p>'; 
      echo '<input type="text" name="_nisn" value="' . $nisn . '" class="widefat" />'; 
      echo '<p>Relationship</p>'; ?> 

      <select name="_rel" id="_rel"> 
       <option value="">Relationship</option> 
       <option value="Single" <?php selected($rel, 'Single'); ?>>Single</option> 
       <option value="Marry" <?php selected($rel, 'Marry'); ?>>Marry</option> 
      </select> 

      <?php 
    } 

    function mhs_data_meta($post_id, $post) { 
     if (!wp_verify_nonce($_POST['eventmeta_noncename'], plugin_basename(__FILE__))) { 
     return $post->ID; 
     } 
     if (!current_user_can('edit_post', $post->ID)) 
      return $post->ID; 

     $events_meta['_nisn'] = $_POST['_nisn']; 
     $events_meta['_rel'] = $_POST['_rel']; 

     foreach ($events_meta as $key => $value) { 
      if($post->post_type == 'revision') return; 
      $value = implode(',', (array)$value); 
      if(get_post_meta($post->ID, $key, FALSE)) { 
       update_post_meta($post->ID, $key, $value); 
      } else { 
       add_post_meta($post->ID, $key, $value); 
      } 
      if(!$value) delete_post_meta($post->ID, $key); 
     } 
    } 

    add_action('save_post', 'mhs_data_meta', 1, 2); 

请帮我纠正我的代码

+0

我建议你使用http://codestarframework.com/是非常容易和强大的框架,以节省时间在这种东西 – efirvida

+0

我必须学习代码和它如何工作,感谢您的答案,但我认为你喜欢垃圾评论(评论包含链接),并没有给我正确的解决方案来学习代码 – Yayun

回答

0

使用Codestar Framework它很简单TU添加metaboxes到您的自定义后,创建配置页面,插件,分类和如果您使用定制更喜欢用它来代替配置页面。请参阅文档here了解更多信息。

在你的榜样,增加一个选择metabox shuld使用类似如下的代码:

负载框架上的的functions.php

require_once __DIR__ . '/'.$FRAMEWORK_PATH.'/cs-framework/cs-framework.php'; 

编辑CS-framekork.php配置文件只激活您需要的功能:

defined('CS_ACTIVE_FRAMEWORK') or define('CS_ACTIVE_FRAMEWORK', false); // if you need it for plugin or configuration page 
defined('CS_ACTIVE_METABOX') or define('CS_ACTIVE_METABOX', true);  // if you only need it for metabox 
defined('CS_ACTIVE_TAXONOMY') or define('CS_ACTIVE_TAXONOMY', false); 
defined('CS_ACTIVE_SHORTCODE') or define('CS_ACTIVE_SHORTCODE', false); 
defined('CS_ACTIVE_CUSTOMIZE') or define('CS_ACTIVE_CUSTOMIZE', false); 

然后在您function.php或者我更喜欢另一个文件包含在你的function.php注册metabox

function register_this_metabox($options) 
{ 

    $options = array(); // this will clean the default cs-framework configuration 

    $options[] = array(
     'id' => 'the_metabox', 
     'title' => 'Meta Box title', 
     'post_type' => 'post', // the post type where the metabox appears 
     'context' => 'side', // metabox position 
     'priority' => 'high', 
     'sections' => array( // metabox fields, see the documentation 
      array(
       'name' => 'the_metabox_fields', 
       'fields' => array(
        array(
         'id' => 'the_metabox_select_field', 
         'type'   => 'select', 
         'title'   => 'Select Field', 
         'options'  => array(
          'opt1' => 'Option 1', 
          'opt2' => 'Option 2', 
          'opt3' => 'Option 3', 
         ), 
         'default_option' => 'Select a option', 
        ), 
       ), 
      ), 
     ), 
    ); 
    return $options; 
} 

add_filter('cs_metabox_options', 'register_this_metabox'); 

而现在你只需要得到您的主题值:

$post_metabox = get_post_meta($post->ID, 'the_metabox', true); 
$selected_option = $post_metabox['the_metabox_select_field'] 
相关问题