首页 > 解决方案 > 检索图像附件的替代文本?

问题描述

我很难弄清楚如何获取附件图像的 alt 属性。我尝试了 8 个代码,但没有一个显示 alt 属性

看起来这个 WP 主题正在使用gallery-metabox插件。

<?php if ( !empty($screenshots) ) { ?>
<div class="screenshotspgcntn col-md-6">
<h4>Images <?php the_title(); ?></h4>
<div class="allphotosgallery clearfix">
<?php if($screenshots) : foreach ($screenshots as $key => $value) : 
$image = wp_get_attachment_image_src($value, 'thumbnail');
$url = wp_get_attachment_image_src($value, 'large');
        
/* I tried all of these:  */
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true);
$image_alt = get_post_meta( $image->id, '_wp_attachment_image_alt', true);
$image_alt = $img_meta['alt'] == '' ? $image_title : $img_meta['alt'];
$image_alt = get_post_meta($imgID,'_wp_attachment_image_alt', true);
$image_alt = get_post_meta( $image->id, '_wp_attachment_image_alt', true);
$image_alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
?>

<div class="perfundo">
<a href="#lightbox-image">
<img src="<?php echo $image[0]; ?>" alt="<?php echo $image_alt; ?>">
</a>
<div id="lightbox-image" class="perfundo__overlay fadeIn">
<figure class="perfundo__figure">
<img src="<?php echo $image[0]; ?>">
<div class="perfundo__image" style="width: 800px; padding-top: 66.25%; background-image: url(<?php echo $url[0]; ?>);"></div>
</figure>
<a href="#image-closed" class="perfundo__close perfundo__control">Close</a>
</div>
</div>
<?php endforeach; endif; ?>
</div>
</div>
<?php } ?>

标签: imageimage-galleryalt

解决方案


推荐阅读