在WordPress中显示图库时,将ALT标记添加到数组中

How would I go about adding an ALT tag for these images? Would I have to add the

$alt = get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true);

to the array, and then add the <?php echo $alt; ?> when displaying the image?

  <?php /* 2 IMAGES DIVIDER */ ?>
    <?php if($divider_type == 'images'): ?>
    <section class='divider content gallery'>
        <?php 
        $images = array();
        foreach($section['images'] as $image_id => $image_url)
        {
            $image = wp_get_attachment_image_src($image_id, '900w');
            $image_details = get_post($image_id);
            $images[] = array('img_url'=>$image[0],'caption'=>$image_details->post_excerpt);
        }
        ?>
        <div class='gallery-half'>
            <figure>
                <img src='<?php echo $images[0]['img_url']; ?>'>
                <?php if($images[0]['caption'] != ''): ?>
                <figcaption><?php echo $images[0]['caption']; ?></figcaption>
                <?php endif; ?>
            </figure>
        </div><!--
        --><div class='gallery-half'>
            <img src='<?php echo $images[1]['img_url']; ?>'>
        </div>
    </section>
    <?php endif; ?>

Thank you in advance.

2个回答

Why not use wp_get_attachment_image()? This handles the alt tag for you.

<?php /* 2 IMAGES DIVIDER */ ?>
<?php if ( $divider_type == 'images' ) : ?>
    <section class='divider content gallery'>
        <?php 
        $images = array();
        foreach ( $section['images'] as $image_id => $image_url ) : ?>
            <?php $image_details = get_post( $image_id ); ?>
            <div class='gallery-half'>
                <figure>
                    <?php echo wp_get_attachment_image( $image_id, '900w' ); ?>
                    <?php if ( $image_details->post_excerpt ) : ?>
                        <figcaption><?php echo $image_details->post_excerpt; ?></figcaption>
                    <?php endif; ?>
                </figure>
            </div>
       <?php endif; ?>
    </section>
<?php endif; ?>

I modified the snippet slightly so it will loop through all the images in your $section['images'] and output the in a .gallery-half div with the figure element and caption if there is one.

dongren7374
dongren7374 非常感谢你们的帮助! Jrod和@nanodanger,谢谢。
接近 4 年之前 回复
dongwo5589
dongwo5589 我想到了! 我已添加此代码'$ alt = get_post_meta(get_post_thumbnail_id($ post-> ID),'_ wp_attachment_image_alt',true);' 对于“foreach”项,我在数组项中添加了''alt'=> $ alt',并添加了'alt ='<?php echo $ alt; ?>''用于图像src输出。
接近 4 年之前 回复
doujiao2000
doujiao2000 有错误吗? 你能提供一个项目链接吗?
接近 4 年之前 回复
dongtaidai0492
dongtaidai0492 嗨@Jrod,对于迟到的回复感到抱歉。 我只是尝试了你的代码片段,如果我使用它,它就不会在帖子上显示任何内容了。
接近 4 年之前 回复

You can use wp_get_attachment_image too, which will get you the img html tag with the alt attribute. However, you were right, if you want to get the value yourself, you have to use get_post_meta. Actually the wp_get_attachment_image function does the same to access that data.

$default_attr = array(
    'src'   => $src,
    'class' => "attachment-$size_class size-$size_class",
    'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问