doru52911 2014-09-24 08:37
浏览 27
已采纳

如何使用自定义帖子类型中任何帖子的用户输入在wordpress中添加多个元框?

What i am actually trying to do here is :
- I have a custom post type to add videos.
- Now in every video a user can add multiple fields for a video for eg.
-- I have a video on which there will be multiple overlays containing (title, desc, button , link etc) and the number of overlays are completely dynamic so the user should be able to set it for every video.
-- So if the user says 5, then the user should be get 5 title, desc, button fields to add

I can add this statically for a particular amount, but how do i make this dynamic.

Thanks

  • 写回答

1条回答 默认 最新

  • doushui20090526 2014-10-16 21:36
    关注

    You can create your own fields in your plugin, it is not difficult, you just need to know what you want to put in your post type ... To add your fields you need to know jQuery to add more fields dynamically... No need for another plugin created because you'd have many things that would use...

    <?php 
    add_meta_box( "my-new-meta", "Video Options", "metas_form", "your_post_type_name", "normal", "high")
    function metas_form(  $post ){
        $post_id = $post->ID;
        $meta_value = get_post_meta( $post_id, 'meta_key_text', true ); 
        ?>
        <div>
            <label id="my_field">Name</label>
            <input type="text" id="my_field" name="my_field" value="<?php echo $meta_value ?>">
        </div>
        <input type="hidden" name="field_nonce" id="field_nonce" value="<?php echo wp_create_nonce( plugin_basename(__FILE__) ); ?>" />
        <?php
    }
    
    add_action( 'save_post', 'save_fn' );
    function save_fn( ) {
        global $post, $post_id, $typenow;
        if ( $typenow  == 'your_post_type_name' && isset( $_POST ) ) {
            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
    
        if ( !isset( $_POST['field_nonce'] ) || !wp_verify_nonce( $_POST['field_nonce'], plugin_basename(__FILE__) )) return $post_id;
        $meta_value = $_POST['my_field'];
        update_post_meta( $post->ID, 'meta_key_text', $meta_value );
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?