2017-02-15 5 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', ''); 
} 

を、私はそれがほとんど動作します言ったように - すべてを意味する - それが「メタチェックボックスを-2」と呼ばれるすべてのものを保存しますこれは目標ではありません。

これは私が迷子になっているところです。私は各名前と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; 
?> 
関連する問題