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 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?