【问题标题】:Add ALT tag to the array in displaying image gallery in WordPress在 WordPress 中显示图片库的数组中添加 ALT 标签
【发布时间】:2016-06-02 18:05:49
【问题描述】:

如何为这些图片添加 ALT 标签?我必须添加

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

到数组中,然后在显示图片的时候加上<?php echo $alt; ?>

  <?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; ?>

提前谢谢你。

【问题讨论】:

    标签: php wordpress image text alt


    【解决方案1】:

    为什么不使用wp_get_attachment_image()?这会为您处理 alt 标签。

    <?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; ?>
    

    我稍微修改了 sn-p,使其循环遍历 $section['images'] 中的所有图像,并在 .gallery-half div 中输出带有图形元素和标题(如果有的话)。

    【讨论】:

    • 嗨@Jrod,很抱歉回复晚了。我刚刚试过你的代码 sn-p,如果我使用它,它不会再在帖子上显示任何内容。
    • @im2shae 有什么错误吗?你能提供一个你的项目的链接吗?
    • 我想通了!我已添加此代码 '$alt = get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true);'在“foreach”项中,我已将 ''alt'=>$alt' 添加到数组项中,并添加了此 'alt=''' 用于图像 src 输出。
    • 非常感谢大家的帮助! Jrod 和@nanodanger,谢谢。
    【解决方案2】:

    您也可以使用wp_get_attachment_image,这将为您提供带有alt 属性的img html 标记。但是,您是对的,如果您想自己获取价值,则必须使用get_post_meta。实际上,wp_get_attachment_image 函数在访问该数据时也是如此。

    $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
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多