CMB2 图片库
CMB2 Photo gallery
当创建 CMB2 file_list 以上传图片以填充图库时,CMB2 在线示例缺少显示选项(例如 img alt 标签和向图片添加 classes 的选项。我不知道如何访问图像,只能通过下面提供的代码访问。例如,我需要向图库中的第一张图片添加 class 并添加 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++;
}
}
当创建 CMB2 file_list 以上传图片以填充图库时,CMB2 在线示例缺少显示选项(例如 img alt 标签和向图片添加 classes 的选项。我不知道如何访问图像,只能通过下面提供的代码访问。例如,我需要向图库中的第一张图片添加 class 并添加 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++;
}
}