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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度