2017-02-15 14 views
0

Wordpress 메타 박스에서 동적으로 생성 된 체크 박스의 데이터를 저장하려고합니다. 지금은 거의 작동합니다 - 그러나 각 체크 박스는 나중에 사용되는 동일한 이름과 ID를가집니다. 따라서 그렇게 될 수는 없습니다. 여기루프에서 생성 된 메타 박스 체크 박스의 데이터 저장

<?php 
      $args = array('post_type' => 'teachers'); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post(); 
      ?> 
      <label for="meta-checkbox-two"> 
       <input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php if (isset ($prfx_stored_meta['meta-checkbox-two'])) checked($prfx_stored_meta['meta-checkbox-two'][0], 'yes'); ?> /> 
       <?php the_title() ?> 
      </label> 
    <?php endwhile; ?> 

및 저장 있어요 : - 그것은 "메타 체크 박스 개의"라고 모든 것을 저장 - 의미 다

// Checks for input and saves 
if(isset($_POST[ 'meta-checkbox-two' ])) { 
    update_post_meta($post_id, 'meta-checkbox-two', 'yes'); 
} else { 
    update_post_meta($post_id, 'meta-checkbox-two', ''); 
} 

내가 거의 작동 말했듯이

이 내가 체크 박스를 생성하는 방법이다 이것은 목표가 아닙니다.

내가 잃어버린 곳입니다. 루프가 검색하는 게시물 ID로 각 이름과 ID를 끝내려고합니다. 여기 코드는 다음 보이는 방법은 다음과 같습니다

생성 체크 박스 : 그들을 저장

<?php 
      $args = array('post_type' => 'teachers'); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post(); 
      ?> 
      <label for="meta-checkbox-two"> 
       <input type="checkbox" name="meta-checkbox-<?php the_ID() ?>" id="meta-checkbox-<?php the_ID() ?>" value="yes" <?php if (isset ($prfx_stored_meta['meta-checkbox-' . the_ID()])) checked($prfx_stored_meta['meta-checkbox-'] . the_ID(), 'yes'); ?> /> 
       <?php the_title() ?> 
      </label> 
    <?php endwhile; ?> 

:

$args = array('post_type' => 'teachers'); 
$loop = new WP_Query($args); 
while ($loop->have_posts()) : $loop->the_post(); 

// Checks for input and saves 
if(isset($_POST[ 'meta-checkbox-'.the_ID()])) { 
    update_post_meta($post_id, 'meta-checkbox-'.the_ID(), 'yes'); 
} else { 
    update_post_meta($post_id, 'meta-checkbox-'.the_ID(), ''); 
} 
endwhile; 

그러나 두 번째 경우에 데이터가 저장되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

0

그냥 테스트 해보 겠지만 작동해야합니다 - name="meta-checkbox-two[]"을 볼 수있는 것처럼 배열의 입력 이름을 변경했습니다. 그 점은 사용자가 입력 한 것과 같은 고유 입력 ID이기도합니다.

<?php 
    $args = array('post_type' => 'teachers'); 
    $loop = new WP_Query($args); 
    while ($loop->have_posts()) : $loop->the_post(); 
    ?> 
     <label for="meta-checkbox-two"> 
      <input type="checkbox" name="meta-checkbox-two[]" id="meta-checkbox-two-<?php the_ID() ?>" value="yes" <?php if (isset ($prfx_stored_meta['meta-checkbox-two'])) checked($prfx_stored_meta['meta-checkbox-two'][0], 'yes'); ?> /> 
      <?php the_title() ?> 
     </label> 
<?php 
    endwhile; 
?>