Wordpress Gallery,如何动态更新附件上传中的自定义选择框

时间:2014-11-03 20:30:15

标签: wordpress

我已在默认图库设置中添加了一个选择框音频选项。这是我添加到functions.php文件中的内容:

    <?php
      function add_custom_gallery_settings() {

        global $post;
        $attached_audio = get_attached_media( 'audio', $post->ID );

    ?>
    <script type="text/html" id="tmpl-custom-gallery-settings">
      <label class="setting">
       <span><?php _e('Audio'); ?></span>
       <select data-setting="audio">
        <option value="">No Audio</option>
       <?php foreach($attached_audio as $a): ?>
          <option value="<?php esc_attr_e($a->ID); ?>">
              <?php echo $a->post_name; ?>
          </option>
       <?php endforeach; ?>
       </select>
      </label>
    </script>
    <script>

      jQuery(document).ready(function(){

        wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({

         render: function() {

           wp.media.view.Settings.prototype.render.apply(this, arguments);

           this.$el.append(wp.media.template('custom-gallery-settings'));

           wp.media.gallery.defaults.audio = null;

           this.update.apply( this, ['audio'] );

           return this;
         }

       });

     });

    </script>
    <?php }

      add_action('print_media_templates', 'add_custom_gallery_settings');

    ?>

如您所见,选择上传到帖子的音频附件框。因为我在print_media_templates挂钩上生成了这些模板,所以上传到帖子的任何音频文件都不会显示,直到我更新/保存帖子和/或重新加载页面。问题是,在上传音频附件时动态更新这些选项的好方法是什么?

0 个答案:

没有答案