2017-02-15 3 views
0

私はワードプレステーマを開発しています。私は2つのカスタムポストタイプ(教師とコース)を登録しました。そのうちの1つ(コース)にいくつかのメタボックスを追加しました。メタボックス - チェックボックスとテキストを合わせて

これらのうち2つはテキスト入力代謝物で、正常に機能します(値は公開時に保存されます)。

もう1つのチェックボックスを追加して、他のカスタム投稿タイプ(教師)からすべての投稿のタイトルとIDを取得したいと思います。ポイントは、特定のコースに関連する教師(または教師)を得ることができることです - ユーザーは、与えられたコースを教える教師の名前でボックスをチェックします。

これまではチェックボックスを表示することができましたが、今のところデータは保存されません。

は、これは今のところどのように見えるかです:

function wpt_languagecourses_location() { 
     global $post; 

    // Noncename needed to verify where the data originated 
     echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
     wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 

    // Get the location data if its already been entered 
     $location = get_post_meta($post->ID, '_location', true); 
     $dresscode = get_post_meta($post->ID, '_dresscode', true); 



    // Echo out the field 
     echo '<p>Course price</p>'; 
     echo '<input type="text" name="_location" value="' . $location . '" class="widefat" />'; 
     echo '<p>How long will the course last?</p>'; 
     echo '<input type="text" name="_dresscode" value="' . $dresscode . '" class="widefat" />';?> 
     <?php 
     $args = array('post_type' => 'teachers'); 

     $loop = new WP_Query($args); 
     ?> 
     <p> 
      <span class="prfx-row-title">Who is the teacher?</span> 
      <div class="prfx-row-content"> 


       <?php while ($loop->have_posts()) : $loop->the_post();?> 

        <label for="meta-checkbox"> 
         <input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if (isset ($prfx_stored_meta['meta-checkbox'])) checked($prfx_stored_meta['meta-checkbox'][0], 'yes'); ?> /> 
         <?php the_title() ?> 
        </label> 

       <?php endwhile; ?> 
      </div> 
     </p> 


     <?php } 

    // Add the Events Meta Boxes 

     function add_languagecourses_metaboxes() { 
      add_meta_box('wpt_languagecourses_location', 'Languagecourses Location', 'wpt_languagecourses_location', 'languagecourses', 'side', 'default'); 
     } 


    // Save the Metabox Data 

     function wpt_save_languagecourses_meta($post_id, $post) { 

    // verify this came from the our screen and with proper authorization, 
    // because save_post can be triggered at other times 
      if (!wp_verify_nonce($_POST['eventmeta_noncename'], plugin_basename(__FILE__))) { 
       return $post->ID; 
      } 

    // Is the user allowed to edit the post or page? 
      if (!current_user_can('edit_post', $post->ID)) 
       return $post->ID; 

    // OK, we're authenticated: we need to find and save the data 
    // We'll put it into an array to make it easier to loop though. 

      $languagecourses_meta['_location'] = $_POST['_location']; 
      $languagecourses_meta['_dresscode'] = $_POST['_dresscode']; 


    // Add values of $events_meta as custom fields 

    foreach ($languagecourses_meta as $key => $value) { // Cycle through the $events_meta array! 
    if($post->post_type == 'revision') return; // Don't store custom data twice 
    $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) 
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value 
     update_post_meta($post->ID, $key, $value); 
    } else { // If the custom field doesn't have a value 
    add_post_meta($post->ID, $key, $value); 
    } 
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank 
    } 

    } 
    // Checks for input and saves 


    add_action('save_post', 'wpt_save_languagecourses_meta', 1, 2); // save the custom fields 

がどのように私は、ユーザーがそれらをチェックチェックボックスからのデータを保存することができますか?

答えて

0

jQueryを少し使用します。だからそれがチェックされるときには、name属性と、新しい値を割り当てるためにチェックしたときに、それを割り当てます。あなたはちょうど私が知っているjQueryのコードが必要な場合は

<input type="checkbox" name="meta_checkbox_checked[]" /> // checked 
<input type="checkbox" name="meta_checkbox_notchecked[]" /> // not checked 

はその後

if($_POST['meta_checkbox_checked']) { 
    $i = 0; 
    foreach($_POST['meta_checkbox_checked']) { 
     $metabox[$i] = $_POST['varient_data'][$i]; 
     $i++; 
    } 
    update_post_meta($post_id, "meta_checkbox_checked", $metabox); 
} 

を保存します。私はこのコードをテストしていないので、私に知らせてください。

関連する問題