Follow us on
  1. In a metabox, gallery field creates an empty image at first, but than other images show good.

    array(
            'id'    => 'gallery_field',
            'type'  => 'gallery',
            'title' => 'Gallery',
          ),CopyCopied!

    This is the code which i call gallery images

    <?php  
    
    $option = get_post_meta( get_the_ID(), 'othemesnet_mb_wc', true );
    $gallery_opt = $option['villa_galerisi']; // for eg. 15,50,70,125
    $gallery_ids = explode( ',', $gallery_opt );
    
    if ( ! empty( $gallery_ids ) ) {
      foreach ( $gallery_ids as $gallery_item_id ) { ?>
    
    <div class="col-md-3 gallery-item_villa">
    <a target="_blank" rel="nofollow" >" data-full="<?php echo wp_get_attachment_url( $gallery_item_id ); ?>">
    <img />" />
      </a>
      </div>
    
      <?php }
    }
    
    ?>CopyCopied!

    This is the output of the problem part:

    <div class="col-md-3 gallery-item_villa">
       <a target="_blank" rel="nofollow"  href="">
          <img src="" />
       </a>
     </div>CopyCopied!

    Actually it creates an empty image and than shows all images clearly.

    <div class="col-md-3 gallery-item_villa">
       <a target="_blank" rel="nofollow"  href="https://site.com/wp-content/uploads/2021/11/villa1.jpg">
         <img src="https://site.com/wp-content/uploads/2021/11/villa1.jpg" />
      </a>
     </div>CopyCopied!
    Posted 1 year ago #

  2. Codestar
    Admin

    Hi,

    Sorry for delay.

    You should add a condition for at first. For eg:

    <?php
    $option      = get_post_meta( get_the_ID(), 'othemesnet_mb_wc', true );
    $gallery_opt = $option['villa_galerisi']; // for eg. 15,50,70,125
    $gallery_ids = explode( ',', $gallery_opt );
    
    if ( ! empty( $gallery_ids ) ) {
      foreach ( $gallery_ids as $gallery_item_id ) {
    
        $img_full = wp_get_attachment_url( $gallery_item_id );
        $img_src  = wp_get_attachment_image_src( $gallery_item_id, 'thumbnail' ); // or medium
    
        if ( empty( $img_full ) || empty( $img_src ) ) {
          continue;
        }
    
        echo '<div class="col-md-3 gallery-item_villa">';
          echo '<a target="_blank" rel="nofollow" data-full="'. esc_html( $img_full ) .'">';
            echo '<img src="'. esc_html( $img_src ) .'" />';
          echo '';
        echo '</div>';
    
      }
    
    }CopyCopied!

    Or check the code via https://gist.github.com/Codestar/54993b8ad8f1ea372612f47d5278da1b

    Regards, Codestar

    Posted 1 year ago #