首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CMB2图片库

CMB2图片库
EN

Stack Overflow用户
提问于 2019-05-19 01:45:41
回答 1查看 379关注 0票数 0

在为图片上传创建CMB2 file_list以填充图库时,CMB2 online示例显示选项,如img alt标签和向图像添加类。我不知道如何访问图像,但只能通过下面提供的代码。例如,我需要向图库中的第一个图像添加一个类,并添加alt标记?如果有人能帮助我,我将不胜感激!

代码语言:javascript
复制
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', '');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-20 12:11:43

您可以为wp_get_attachment_image()中的第四个参数传递条件实参。第四个参数用于自定义属性。在该函数中,仅为第一个图像添加自定义属性。请查看以下示例。

代码语言:javascript
复制
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++;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56201419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档