2017-02-15 4 views
0

나는 워드 프레스 테마를 개발 중이다. 두 가지 맞춤 게시 유형 (교사 및 코스)을 등록했으며 그 중 하나 (코스)에 몇 가지 메타 복스를 추가했습니다.메타 박스 - 체크 박스와 텍스트 함께.

두 개는 텍스트 입력 대사이며 잘 작동합니다 (값은 게시시 저장됩니다).

이제 다른 맞춤 게시물 유형 (교사)의 모든 게시물의 제목과 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 속성과 when 우리가 새로운 값을 할당하기 위해 체크했을 때를 할당합니다. 그냥 알려 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); 
} 

을 저장합니다. 이 코드를 테스트하지 않았으므로 알려주십시오.