douyanjing8287 2013-06-17 18:13
浏览 15
已采纳

在wordpress帖子中“在运行中”创建元字段

Is there any way to have a registered meta box dynamically appear numerous times based what is selected/entered in another meta box or taxonomy field?

i.e. my custom post needs to have a variable amount of "sections" (text fields). There would be no way for me to pre-define how many that would be; only during data entry can that amount be known. These fields would then be also dynamically added to the post template on display.

  • 写回答

2条回答 默认 最新

  • douxing8939 2013-06-18 21:20
    关注

    After registering my custom post also called post and a metabox with callback "display_post_meta_box" this worked:

    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++; }
    
    }
    

    /Saves Post/

      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'] );
            }
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址