WordPress媒体库未在Gridview中显示旧图像,列表视图显示图像

时间:2019-07-01 17:38:50

标签: wordpress

我已经创建了一个脚本来为所有类别插入自定义字段图像,并且数据库条目也可以。

现在,当我转到媒体库时,它仅加载我的最新图像,而旧图像则不显示。微调器继续旋转,并且Ajax调用变为无限。

2 个答案:

答案 0 :(得分:0)

有很多原因会使旧的图像加载失败。请打开“调试模式”并检查“调试日志”并在此处共享信息,以便我们为您提供帮助。

答案 1 :(得分:0)

<?php

add_action( 'business_add_form_fields', 'add_category_image', 10, 2 );
add_action( 'created_business', 'save_category_image', 10, 2 );
add_action( 'business_edit_form_fields','update_category_image', 10, 2 );
add_action( 'edited_business','updated_category_image', 10, 2 );
add_action( 'admin_enqueue_scripts',  'load_media');
add_action( 'admin_footer', 'add_script');

function load_media() {
    if( ! isset( $_GET['taxonomy'] ) || $_GET['taxonomy'] != 'business' ) {
      return;
    }
    wp_enqueue_media();
}
function add_category_image( $taxonomy ) { ?>
    <div class="form-field term-group">
        <label for="showcase-taxonomy-extra-detail-id"><?php _e( 'Business Extra Details', 'launchpm' ); ?></label>
        <input type="text" id="showcase-taxonomy-extra-detail-id" name="showcase-taxonomy-extra-detail-id" class="" value="">
    </div>
    <div class="form-field term-group">
      <label for="showcase-taxonomy-image-id"><?php _e( 'Image', 'launchpm' ); ?></label>
      <input type="hidden" id="showcase-taxonomy-image-id" name="showcase-taxonomy-image-id" class="custom_media_url" value="">
      <div id="category-image-wrapper"></div>
      <p>
        <input type="button" class="button button-secondary showcase_tax_media_button" id="showcase_tax_media_button" name="showcase_tax_media_button" value="<?php _e( 'Add Image', 'launchpm' ); ?>" />
        <input type="button" class="button button-secondary showcase_tax_media_remove" id="showcase_tax_media_remove" name="showcase_tax_media_remove" value="<?php _e( 'Remove Image', 'launchpm' ); ?>" />
      </p>
    </div>
  <?php }
  /**
 * Edit the form field
 * @since 1.0.0
 */
function update_category_image( $term, $taxonomy ) { ?>
    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="showcase-taxonomy-extra-detail-id"><?php _e( 'Business Extra Details', 'launchpm' ); ?></label>
        </th>
        <td>
            <?php $extra_detail = get_term_meta( $term->term_id, 'showcase-taxonomy-extra-detail-id', true ); ?>
            <input type="text" id="showcase-taxonomy-extra-detail-id" name="showcase-taxonomy-extra-detail-id" value="<?php echo  $extra_detail ; ?>">
        </td>
    </tr>
    <tr class="form-field term-group-wrap">
        <th scope="row">
        <label for="showcase-taxonomy-image-id"><?php _e( 'Image', 'launchpm' ); ?></label>
        </th>
        <td>
        <?php $image_id = get_term_meta( $term->term_id, 'showcase-taxonomy-image-id', true ); ?>
        <input type="hidden" id="showcase-taxonomy-image-id" name="showcase-taxonomy-image-id" value="<?php echo esc_attr( $image_id ); ?>">
        <div id="category-image-wrapper">
            <?php if( $image_id ) { ?>
            <?php echo wp_get_attachment_image( $image_id, 'thumbnail' ); ?>
            <?php } ?>
        </div>
        <p>
            <input type="button" class="button button-secondary showcase_tax_media_button" id="showcase_tax_media_button" name="showcase_tax_media_button" value="<?php _e( 'Add Image', 'showcase' ); ?>" />
            <input type="button" class="button button-secondary showcase_tax_media_remove" id="showcase_tax_media_remove" name="showcase_tax_media_remove" value="<?php _e( 'Remove Image', 'showcase' ); ?>" />
        </p>
        </td>
    </tr>
    <?php }
  /**
    * Save the form field
    * @since 1.0.0
    */
    function save_category_image( $term_id, $tt_id ) {
        if( isset( $_POST['showcase-taxonomy-image-id'] ) && '' !== $_POST['showcase-taxonomy-image-id'] ){
          add_term_meta( $term_id, 'showcase-taxonomy-image-id', absint( $_POST['showcase-taxonomy-image-id'] ), true );
        }
        if( isset( $_POST['showcase-taxonomy-extra-detail-id'] ) && '' !== $_POST['showcase-taxonomy-extra-detail-id'] ){
          add_term_meta( $term_id, 'showcase-taxonomy-extra-detail-id',  $_POST['showcase-taxonomy-extra-detail-id'] , true );
        }
       }
  /**
    * Update the form field value
    * @since 1.0.0
    */
    function updated_category_image( $term_id, $tt_id ) {
        if( isset( $_POST['showcase-taxonomy-image-id'] ) && '' !== $_POST['showcase-taxonomy-image-id'] ){
          update_term_meta( $term_id, 'showcase-taxonomy-image-id', absint( $_POST['showcase-taxonomy-image-id'] ) );
        } else {
          update_term_meta( $term_id, 'showcase-taxonomy-image-id', '' );
        }
        if( isset( $_POST['showcase-taxonomy-extra-detail-id'] ) && '' !== $_POST['showcase-taxonomy-extra-detail-id'] ){
          update_term_meta( $term_id, 'showcase-taxonomy-extra-detail-id',  $_POST['showcase-taxonomy-extra-detail-id']  );
        } else {
          update_term_meta( $term_id, 'showcase-taxonomy-extra-detail-id', '' );
        }
      }
    /**
    * Enqueue styles and scripts
    * @since 1.0.0
    */
   function add_script() {
    if( ! isset( $_GET['taxonomy'] ) || $_GET['taxonomy'] != 'business' ) {
      return;
    } ?>
    <script> jQuery(document).ready( function($) {
      _wpMediaViewsL10n.insertIntoPost = '<?php _e( "Insert", "showcase" ); ?>';
      function ct_media_upload(button_class) {
        var _custom_media = true, _orig_send_attachment = wp.media.editor.send.attachment;
        $('body').on('click', button_class, function(e) {
          var button_id = '#'+$(this).attr('id');
          var send_attachment_bkp = wp.media.editor.send.attachment;
          var button = $(button_id);
          _custom_media = true;
          wp.media.editor.send.attachment = function(props, attachment){
            if( _custom_media ) {
              $('#showcase-taxonomy-image-id').val(attachment.id);
              $('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
              $( '#category-image-wrapper .custom_media_image' ).attr( 'src',attachment.url ).css( 'display','block' );
            } else {
              return _orig_send_attachment.apply( button_id, [props, attachment] );
            }
          }
          wp.media.editor.open(button); return false;
        });
      }
      ct_media_upload('.showcase_tax_media_button.button');
      $('body').on('click','.showcase_tax_media_remove',function(){
        $('#showcase-taxonomy-image-id').val('');
        $('#category-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
      });

      $(document).ajaxComplete(function(event, xhr, settings) {
        var queryStringArr = settings.data.split('&');
        if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
          var xml = xhr.responseXML;
          $response = $(xml).find('term_id').text();
          if($response!=""){
            // Clear the thumb image
            $('#category-image-wrapper').html('');
          }
         }
       });
     });
   </script>
  <?php }