自定义元框重复字段

时间:2015-03-22 作者:NinjaMAN

我想在我的自定义元框中添加一个字段,如下所示,但不用于标记。如何在同一字段中重复获取更多输入?

enter image description here

下面是我添加到函数中的代码。php:

function dikka_cmb_meoxes( array $meta_boxes ) {
    $prefix = \'dikka_\';
    $meta_boxes[\'details_meox\'] = array(
        \'id\'         => \'details_meox\',
        \'title\'      => __( \'Porject Details\', \'dikka\' ),
        \'pages\'      => array( \'portfolio\', ), // Post type
        \'context\'    => \'normal\',
        \'priority\'   => \'high\',
        \'show_names\' => true, // Show field names on the left
        \'fields\'     => array(
            array(
                \'name\'     => __( \'Client\', \'dikka\' ),
                \'desc\'     => __( \'Add client name\', \'dikka\' ),
                \'id\'       => $prefix . \'add_client\',
                \'type\'     => \'text\',
            ),
            array(
                \'name\'     => __( \'Skills\', \'dikka\' ),
                \'desc\'     => __( \'Add skills\', \'dikka\' ),
                \'id\'       => $prefix . \'skills\',
                \'type\'     => \'text\',
            ),
            array(
                \'name\'     => __( \'Release Date\', \'dikka\' ),
                \'desc\'     => __( \'Add release date of project\', \'dikka\' ),
                \'id\'       => $prefix . \'add_releasedate\',
                \'type\'     => \'text_date\',
            ),
        )
    );
    return $meta_boxes;
}

2 个回复
SO网友:tam

如果看起来您正在使用CMB(WordPress的自定义元框和字段)创建您的创建元框。CMB已经为您提供了一个可以使用的分组和重复元框选项。你可以检查这个link

就您的代码而言,假设它应该是这样的:

function dikka_cmb_meoxes( array $meta_boxes ) {
    $prefix = \'dikka_\';
        $meta_boxes[\'details_meox\'] = array(
        \'id\'          => $prefix . \'details_meox\',
        \'type\'        => \'group\',
        \'pages\'      => array( \'portfolio\', ), // Post type
        \'description\' => __( \'Porject Details\', \'dikka\' ),
        \'options\'     => array(
            \'group_title\'   => __( \'Entry {#}\', \'dikka\' ), // since version 1.1.4, {#} gets replaced by row number
            \'add_button\'    => __( \'Add Another Entry\', \'dikka\' ),
            \'remove_button\' => __( \'Remove Entry\', \'dikka\' ),
            \'sortable\'      => true, // beta
        ),
        // Fields array works the same, except id\'s only need to be unique for this group. Prefix is not needed.
        \'fields\'     => array(
            array(
                \'name\'     => __( \'Client\', \'dikka\' ),
                \'desc\'     => __( \'Add client name\', \'dikka\' ),
                \'id\'       => $prefix . \'add_client\',
                \'type\'     => \'text\',
            ),
            array(
                \'name\'     => __( \'Skills\', \'dikka\' ),
                \'desc\'     => __( \'Add skills\', \'dikka\' ),
                \'id\'       => $prefix . \'skills\',
                \'type\'     => \'text\',
            ),
            array(
                \'name\'     => __( \'Release Date\', \'dikka\' ),
                \'desc\'     => __( \'Add release date of project\', \'dikka\' ),
                \'id\'       => $prefix . \'add_releasedate\',
                \'type\'     => \'text_date\',
            ),
        ),
    ),

    return $meta_boxes;
}
希望这有帮助!

SO网友:Vivek Tamrakar

**具有重复字段的自定义Mata框**

Meta box function with script

function question_creation_meta_box() {
    global $post;

    wp_nonce_field( basename( __FILE__ ), \'bsw_our_nonce\' );

    ?>
    <div class="row">
    <?php
    global $post;
    // $post_id=$post->ID;
    $post_id=$_GET[\'post\'];
    $i=1;
    $countQuestions=get_post_meta($post_id,\'countQuestions\',true);
    // echo \'<pre>\';print_r($countQuestions);echo \'</pre>\';
   $count= count($countQuestions);
    if($post_id){   
      while($i<=$count)
      {
          $question=  get_post_meta($post_id,\'questions\'.$i,true);
          $correctAnswer=get_post_meta($post_id,\'correctAnswer\'.$i.\'1\',true);
          $countAnswers=get_post_meta($post_id,\'countAnswers\'.$i,true);
           // if($count==1):
          ?>
          <div class="questions_blocks col-sm-12 col-md-12" id="questions_blocks-<?php echo $i; ?>">
              <div class="questions col-sm-12 col-md-12" id="questions-<?php echo $i;?>">
                  <lable>Enter Questions</lable><input type="text" value="<?php echo $question[0]; ?>" name="questions1[]">
              </div>
              <?php
                $j=1;
                while($j<=$countAnswers)
                {
                   $answer= get_post_meta($post_id,\'answer\'.$i.\'\'.$j,true);
                   $answerText= get_post_meta($post_id,\'answerText\'.$i.\'\'.$j,true);
                   ?>
                    <div class="answer col-md-12 col-sm-12 border-top-0" id="answer-<?php echo $j;?>">
                        <h1 class="">Answer <?php echo $j;?></h1>
                        <div class="col-md-12 col-sm-12">
                            <div class="col-md-4 col-sm-4">
                            <input type="radio" name="correctAnswer<?php echo $i; ?>1[]" value="1" class="correct_incorrect" <?php if($correctAnswer[0] == $j ) echo \'checked\' ?>>Correct<br>
                            <input type="radio" name="correctAnswer<?php echo $i; ?>1[]" value="0" class="correct_incorrect">InCorrect
                            </div>
                            <div class="col-sm-8 col-md-8">
                            <lable>Enter Answer</lable><input type="text" value="<?php echo $answer[0];?>" name="answer1<?php echo $j; ?>[]">
                            <lable>Enter Display Answer text</lable> <input type="text" value="<?php echo  $answerText[0]?>" name="answerText1<?php echo $j; ?>[]">
                            </div>
                        </div>
                        <input type="hidden" value="1" name="countAnswers1[]">
                        <?php if($j == $countAnswers): ?>
                        <label class="right-label">Add Another Answer</label><a href="javascript:void(0)" onclick="add_answer(this)" class="add_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>
                        <a href="javascript:void(0)" onclick="remove_answer(this)" class="remove_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/remove.png\'?>"></a>
                      <?php endif;?>
                    </div>
                   <?php
                   $j++;
                }
              ?>
              <input type="hidden" value="1" name="countQuestions[]">
              <?php if($count == $i ): ?>
              <label class="right-label">Add Another Question</label>
              <a href="javascript:void(0)" onclick="add_question(this)" class="add_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>
              <a href="javascript:void(0)" onclick="remove_question(this)" class="remove_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/remove.png\'?>"></a>
            <?php endif;?>
          </div>
        <?
            $i++;
      }
    }
    else{
    ?>
    <!-- my custom value input -->
      <div class="questions_blocks col-sm-12 col-md-12" id="questions_blocks-1">
          <div class="questions col-sm-12 col-md-12" id="questions-1">
              <lable>Enter Questions</lable><input type="text" value="" name="questions1[]">
          </div>
          <div class="answer col-md-12 col-sm-12 border-top-0" id="answer-1">
              <h1 class="">Answer 1</h1>
              <div class="col-md-12 col-sm-12">
                  <div class="col-md-4 col-sm-4">
                  <input type="radio" name="correctAnswer11[]" value="1" class="correct_incorrect">Correct<br>
                  <input type="radio" name="correctAnswer11[]" value="0" class="correct_incorrect">InCorrect
                  </div>
                  <div class="col-sm-8 col-md-8">
                  <lable>Enter Answer</lable><input type="text" value="" name="answer11[]">
                  <lable>Enter Display Answer text</lable> <input type="text" name="answerText11[]">
                  </div>
              </div>
              <input type="hidden" value="1" name="countAnswers1[]">
              <label class="right-label">Add Another Answer</label><a href=javascript:void(0)" onclick="add_answer(this)" class="add_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a></a>
          </div>
          <input type="hidden" value="1" name="countQuestions[]">
          <label class="right-label">Add Another Question</label><a href=javascript:void(0)" onclick="add_question(this)" class="add_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a></a>
      </div>
      <?php
      }
      ?>
    </div>
    <script type="text/javascript">
      function add_answer(obj)
      {
        var parentID=jQuery(obj).parents(\'.answer\').attr(\'id\');
        var questionParentId=jQuery(obj).parent(\'.answer\').parent(\'.questions_blocks\').attr(\'id\');
        var answerLenght=jQuery(\'#\'+questionParentId +\' .answer\').length;
        var questionParent=jQuery(obj).parent(\'.answer\').parent(\'.questions_blocks\').length;
        var countQuestion=questionParentId.split(\'-\');
        // alert(countQuestion);
        var newLenght=parseInt(answerLenght)+1;
        var html=\'<div class="answer col-md-12 col-sm-12 border-top-0" id="answer-\'+newLenght+\'"><h1 class="">Answer \'+newLenght+\'</h1>\';
          html+=\'<div class="col-md-12 col-sm-12 bsw_answer_block">\';
          html+=\'<div class="col-md-4 col-sm-4 correctblock">\';
          html+=\'<input type="radio" name="correctAnswer\'+countQuestion[1]+\'1[]" value="\'+newLenght+\'" class="correct_incorrect">Correct<br>\';
          html+=\'<input type="radio" name="correctAnswer\'+countQuestion[1]+\'1[]" value="0" class="correct_incorrect">InCorrect</div>\';
          html+=\'<div class="col-sm-8 col-md-8">\';
          html+=\'<lable>Enter Answer</lable><input type="text" value="" name="answer\'+countQuestion[1]+\'\'+newLenght+\'[]">\';
          html+=\'<lable>Enter Display Answer text</lable> <input type="text" name="answerText\'+countQuestion[1]+\'\'+newLenght+\'[]"">\';
          html+=\'</div>\';
          html+=\'</div>\';

          html+=\' <input type="hidden" value="1" name="countAnswers\'+countQuestion[1]+\'[]"><label class="right-label">Add Another Answer</label><a href="javascript:void(0)" onclick="add_answer(this)" class="add_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>\';
         html+=\' <a href="javascript:void(0)" onclick="remove_answer(this)" class="remove_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/remove.png\'?>"></a>\';
         html+=\'</div>\';
         jQuery(\'#\'+questionParentId+\' #\'+parentID+\' .right-label\').remove();
         jQuery(\'#\'+questionParentId+\' #\'+parentID+\' .add_answer\').remove();
         jQuery(\'#\'+questionParentId+\' #\'+parentID+\' .remove_answer\').remove();
         jQuery(\'#\'+questionParentId+\' #\'+parentID).after(html);
      }
      function add_question(obj)
      {
        var parentID=jQuery(obj).parents(\'.questions_blocks\').attr(\'id\');
        var questions_blocksLenght=jQuery(\'.row .questions_blocks\').length;
        var newLenght=parseInt(questions_blocksLenght)+1;
        var html=\'\';
          html+=\'<div class="questions_blocks col-sm-12 col-md-12" id="questions_blocks-\'+newLenght+\'">\';
          html+=\'<div class="questions col-sm-12 col-md-12" id="questions-\'+newLenght+\'">\';
          html+=\'<lable>Enter Questions</lable><input type="text" value="" name="questions\'+newLenght+\'[]">\';
          html+=\'</div>\';
          html+=\'<div class="answer col-md-12 col-sm-12 border-top-0" id="answer-1"><h1 class="">Answer 1</h1>\';
          html+=\'<div class="col-md-12 col-sm-12 bsw_answer_block">\';
          html+=\'<div class="col-md-4 col-sm-4 correctblock">\';
          html+=\'<input type="radio" name="correctAnswer\'+newLenght+\'1[]" value="1" class="correct_incorrect">Correct<br>\';
          html+=\'<input type="radio" name="correctAnswer\'+newLenght+\'1[]" value="0" class="correct_incorrect">InCorrect</div>\';
          html+=\'<div class="col-sm-8 col-md-8">\';
          html+=\'<lable>Enter Answer</lable><input type="text" value="" name="answer\'+newLenght+\'1[]">\';
          html+=\'<lable>Enter Display Answer text</lable> <input type="text" name="answerText\'+newLenght+\'1[]">\';
          html+=\'</div>\';
          html+=\'</div>\';

          html+=\' <input type="hidden" value="1" name="countAnswers\'+newLenght+\'[]"><label class="right-label">Add Another Answer</label><a href="javascript:void(0)" onclick="add_answer(this)" class="add_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>\';
         html+=\'</div>\';
         html+=\'<input type="hidden" value="1" name="countQuestions[]"><label class="right-label">Add Another Question</label><a href="javascript:void(0)" onclick="add_question(this)" class="add_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>\';
         html+=\'<a href="javascript:void(0)" onclick="remove_question(this)" class="remove_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/remove.png\'?>"></a>\';
         html+=\'</div>\';
         jQuery(\'#\'+parentID).after(html);
         jQuery(\'#\'+parentID+ \' .remove_question\').remove();
         jQuery(\'#\'+parentID+ \' .add_question\').remove();
         jQuery(\'#\'+parentID+ \' .right-label\').remove();
      }
      function remove_question(obj)
      {
        var parentID=jQuery(obj).parents(\'.questions_blocks\').attr(\'id\');
        var countQustBlock=parentID.split(\'-\');
        var newParent=countQustBlock[1]-1;
        jQuery(\'#\'+parentID).remove();
        var html=\'\';
        if(newParent>1)
        {
          html+=\'<a href="javascript:void(0)" onclick="remove_question(this)" class="remove_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/remove.png\'?>"></a>\';
        }else;
          html+=\'<label class="right-label">Add Another Question</label><a href="javascript:void(0)" onclick="add_question(this)" class="add_question"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>\';

          jQuery(\'#questions_blocks-\'+newParent).append(html);

      }
      function remove_answer(obj)
      {
          var parentID=jQuery(obj).parents(\'.answer\').attr(\'id\');
          var questionParentId=jQuery(obj).parent(\'.answer\').parent(\'.questions_blocks\').attr(\'id\');
          var answerLenght=jQuery(\'#\'+questionParentId +\' .answer\').length;
          var questionParent=jQuery(obj).parent(\'.answer\').parent(\'.questions_blocks\').length;
          var countAnsBlock=parentID.split(\'-\');
          var newParent=countAnsBlock[1]-1;
          var html=\'\';
          if(newParent > 1)
          {
             html+=\' <a href="javascript:void(0)" onclick="remove_answer(this)" class="remove_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/remove.png\'?>"></a>\';
          }else;
           html+=\'<label class="right-label">Add Another Answer</label><a href="javascript:void(0)" onclick="add_answer(this)" class="add_answer"><img src="<?php echo plugin_dir_url( __FILE__ ) .\'img/plus.png\'?>"></a>\';


          jQuery(\'#\'+questionParentId+\' #\'+parentID).remove();
          jQuery(\'#\'+questionParentId+\' #answer-\'+newParent).append(html);
        }
    </script>
    <?php
}

Save Meta box

function bsw_save_meta_fields( $post_id ) {


  if (!isset($_POST[\'bsw_our_nonce\']) || !wp_verify_nonce($_POST[\'bsw_our_nonce\'], basename(__FILE__)))
      return \'nonce not verified\';


  if ( wp_is_post_autosave( $post_id ) )
      return \'autosave\';


  if ( wp_is_post_revision( $post_id ) )
      return \'revision\';


  if ( \'bws_quiz\' == $_POST[\'post_type\'] ) {
      if ( ! current_user_can( \'edit_page\', $post_id ) )
          return \'cannot edit page\';
      } elseif ( ! current_user_can( \'edit_post\', $post_id ) ) {
          return \'cannot edit post\';
  }
  $bws_question_count=$_POST[\'countQuestions\'];
  update_post_meta($post_id,\'countQuestions\',$bws_question_count);
  $count= count($bws_question_count);
  $i=1;
  while($i<=$count)
  {
    $Questvalue=$_POST[\'questions\'.$i];
   // print_r($Questvalue);
    $bws_correct_answer=$_POST[\'correctAnswer\'.$i.\'1\'];
    update_post_meta($post_id,\'correctAnswer\'.$i.\'1\',$bws_correct_answer);
    $bws_ans_value = $_POST[\'countAnswers\'.$i];
    $bws_ans_count= count($bws_ans_value);
    $j=1;

  //  echo \'Answer \';
    while ( $j<= $bws_ans_count) {
       $answervalue=$_POST[\'answer\'.$i.\'\'.$j];
      // print_r($answervalue);
        $answertextvalue=$_POST[\'answerText\'.$i.\'\'.$j];
         update_post_meta($post_id,\'answer\'.$i.\'\'.$j,$answervalue);
         update_post_meta($post_id,\'answerText\'.$i.\'\'.$j,$answertextvalue);
     //  print_r($answertextvalue);
       $j++;
    }
    update_post_meta($post_id,\'questions\'.$i,$Questvalue);
    update_post_meta($post_id,\'correctAnswer\'.$i.\'1\',$bws_correct_answer);
    update_post_meta($post_id,\'countAnswers\'.$i, $bws_ans_count);
    $i++;
  }

  //simply we have to save the data now
}
add_action( \'save_post\', \'bsw_save_meta_fields\' );
add_action( \'new_to_publish\', \'bsw_save_meta_fields\' );

结束

相关推荐

将jQuery日期选择器添加到自定义发布类型Metabox

我已经创建了一个自定义的post类型元数据库,它可以很好地显示和运行。问题在于日期选择器似乎不起作用,当单击元框时,您可以输入它,而不是出现日期选择器。另一方面,我的脚本似乎都无法工作,尽管它们都已加载。编写加载所有脚本的代码。//Making jQuery Google API function modify_jquery() { if (!is_admin()) { // comment out the next two lines to load th