【问题标题】:Create meta fields "on-the-fly" in a wordpress post在 wordpress 帖子中“即时”创建元字段
【发布时间】:2023-03-14 13:23:01
【问题描述】:

有没有办法让注册的元框根据在另一个元框或分类字段中选择/输入的内容动态出现多次?

即我的自定义帖子需要有可变数量的“部分”(文本字段)。我无法预先定义有多少。只有在数据输入期间才能知道该数量。然后这些字段也将动态添加到显示的帖子模板中。

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    我不知道您是否要求提供特定代码来执行上述操作,但如果它更多的是一般性问题,我会推荐 Types plugin

    它有很多功能,但它允许您选择出现在哪些帖子类型组上的自定义字段、它们出现在哪些页面模板上,而且,对于您的问题而言,重要的是,它们还具有基于其他字段值的条件显示。如果没有更具体的问题,我不确定这可能会有多大用处,很抱歉它不是一个代码-y 答案,但希望它有所帮助。

    【讨论】:

      【解决方案2】:

      在注册我的自定义帖子后,也称为 post 和一个带有回调“display_post_meta_box”的元框,这有效:

      function display_post_meta_box( $post ) {
      
              // Retrieve current details of the post based on post ID    
          $section_number = get_post_meta( $post->ID, 'section_number', true ) ;  
          /*Configuration for post section WYSIWYG editors*/
                      $i = 1;
                      while ($i <= $section_number) {
          $section_editor_settings[$i] = array(  'textarea_rows' => '5','media_buttons' => false, 'textarea_name'=>__( 'post_section'.$i ), 'teeny'=>'true' );
          $i++; }
      
      
      
              // Retrieve current details of the post based on post ID
          $section =  get_post_meta( $post->ID, 'section', true );
          $section_number = get_post_meta( $post->ID, 'section_number', true ) ;          
      
          ?>
              <style>.cell{display: table-cell; vertical-align: middle;}
          .row{width: 100%;display: table;border-bottom: 1px solid #DFDFDF;padding: 2px;}
          </style>
      
      
      
              <div class="row">
                                          <div class="cell" style="width: 15%"><b>Number of sections</b></div>
                                          <div class="cell" style="width: 10%"><input type="number" name="post_section_number"  value="<?php echo $section_number; ?>" /></input></div>
                                          <div class="cell" style="width: 5%">        <script>
              jQuery('.metabox_submit').click(function(e) {
                  e.preventDefault();
                  jQuery('#publish').click();
              });
              </script>
              <input type="submit" class="metabox_submit button button-primary" value="Submit" /></div>
                                          <div class="cell" style="width: 70  %"> &nbsp;</div>
                                        </div>
      
      
          <?php 
                      $i = 1;
                      while ($i <= $section_number) {?>
                              <div class="row">
                                          <div class="cell" style="width: 30%"><b>Section <?php echo '#'.$i;?> </b></div>
                                          <div class="cell" style="width: 50%"><?php  wp_editor( $section[$i], 'sectionid'.$i, $section_editor_settings[$i]  ); ?> </div>
                                          <div class="cell" style="width: 5%"></div>
                                          <div class="cell" style="width: 15%"><p class="howto">Enter body for this section here, and apply neccessary formatting</p></div>
                                        </div>
          <?php $i++; }
      
      }
      

      /保存帖子/

        add_action( 'save_post', 'add_post_fields', 10, 2 );  
      
      function add_post_fields( $post_id, $post ) {
          // Check post type for movie reviews
          $section_values[] = array();
      
          if ( $post->post_type == 'post' ) {
              $i = 1;
                      while ($i <= 4) {
      
                          $section_values[$i] = $_POST['post_section'.$i];
      
                          $i++; }
              // Store data in post meta table if present in post data
              if ( isset( $section_values ) && $section_values != '' ) {
                  update_post_meta( $post_id, 'section', $section_values );
              }
      
                  if ( isset( $_POST['post_section_number'] ) && $_POST['post_section_number'] != '' ) {
                  update_post_meta( $post_id, 'section_number', $_POST['post_section_number'] );
              }
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多