duanji2772 2016-06-02 18:05
浏览 75

在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条回答

  • doudang4568 2016-06-02 18:16
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog