简体   繁体   English

CMB2照片库

[英]CMB2 Photo gallery

When creating a CMB2 file_list for images upload to populate a gallery CMB2 online example is laking to show options such as the img alt tag and adding a classes to the images. 当为图像上传创建CMB2 file_list以填充图库时,CMB2在线示例将显示img alt标签和向图像添加类等选项。 I do not know how to access the images but only via the supplied code below. 我不知道如何访问图像,但只能通过下面提供的代码。 I need to add a class to the first image in the gallery for example and adding the alt tag? 我需要在库中的第一个图像中添加一个类,例如添加alt标签? If anyone can help I would be thankful! 如果有人可以提供帮助,我会感激不尽!

function cmb2_output_file_list( $file_list_meta_key, $img_size = '' ) {

// Get the list of files
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );

// Loop through them and output an image
foreach ( (array) $files as $attachment_id => $attachment_url ) {

  echo '<div class="slide">';                   
  echo wp_get_attachment_image( $attachment_id, $img_size);
  echo '</div>';                    
  }             
}                   
cmb2_output_file_list( 'bs_bautage_pic', '');

You can pass conditional arguments for fourth parameter in wp_get_attachment_image() . 您可以在wp_get_attachment_image()传递第四个参数的条件参数。 This fourth parameter is for the custom attributes. 第四个参数用于自定义属性。 In the function, custom attributes are added for the first image only. 在该函数中,仅为第一个图像添加自定义属性。 Please check following example. 请查看以下示例。

function cmb2_output_file_list( $file_list_meta_key, $img_size = '' ) {
    $files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );

    $counter = 0;
    foreach ( (array) $files as $attachment_id => $attachment_url ) {
        echo '<div class="slide">';
        $args = array();
        if ( 0 === $counter ) {
            $args = array(
                'alt'   => 'Sample Text',
                'class' => 'custom-class',
                );
        }
        echo wp_get_attachment_image( $attachment_id, $img_size, false, $args );
        echo '</div>';
        $counter++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM