wordpress - CMB2 照片库
问题描述
在为图片上传创建 CMB2 file_list 以填充图库时,CMB2 在线示例无法显示诸如 img alt 标签之类的选项并向图片添加类。我不知道如何访问图像,但只能通过下面提供的代码。例如,我需要向图库中的第一张图片添加一个类并添加 alt 标签?如果有人可以提供帮助,我将不胜感激!
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', '');
解决方案
您可以在wp_get_attachment_image()
. 第四个参数用于自定义属性。在该函数中,仅为第一张图像添加自定义属性。请检查以下示例。
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++;
}
}
推荐阅读
- excel - 使用 VBA 访问网页对话框
- sql - 数据库设计 - 3个表之间的关系
- ios - 当我在 Swift 中从同一部 iPhone 中删除应用程序时,为什么我的唯一设备 ID 会发生变化?
- openssl - 如何为 sslip.io 创建自签名 SSL 证书并将浏览器配置为信任它
- php - PHPSTAN 字符串和3600之间的二进制操作“*”导致错误
- java - 给定一个字符串,生成所有 2 个连续的字符、3 个连续的字符……等等直到 ( str.length()-1 ) 个连续的字符
- electron - 在 GitlabCI 上运行测试时 ChromeDriver 出现问题
- python - 如何在不使用终端的情况下使用 PyCharm 安装 Matplotlib?
- angular - 如何正确处理这个 Angular 承诺以返回一个对象数组?
- react-native - 拍摄后在 facebook 上分享 - 不在 facebook 上显示图像