【问题标题】:Adding alt text to images within a wordpress slider在 wordpress 滑块中向图像添加替代文本
【发布时间】:2021-01-07 23:05:09
【问题描述】:

我希望将 Alt 文本添加到滑块图像。我能够添加该行:

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

这条线拉入了照片的替代文字,但它与滑块中所有照片的替代文字相同。它似乎没有将每个替代文本循环到每张照片。下面是拉取图片的代码。

<?php $_gallery = get_field('project_gallery_photos'); ?>

        <?php if( $_gallery ) { ?>
            <div class="_featured-images">
                <div class="_slider">
                    <?php foreach( $_gallery as $_img ): ?>
                        <div class="_slide">
                            <img src="<?= $_img['photo']['sizes']['project-images']?>" alt ="<?= $alt = get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>"/>

                            <?php if( $_img['photo_title'] || $_img['photo_caption'] ) { ?>
                                <div class="_caption">
                                    <?= $_img['photo_title']? '<b>'.$_img['photo_title'].'</b><br>': '' ?>
                                    <?= $_img['photo_caption'] ?>
                                </div>
                            <?php } ?>
                        </div>
                    <?php endforeach; ?>
                </div>

【问题讨论】:

    标签: php wordpress image alt


    【解决方案1】:

    您作为当前全局 $post 的“特色图片”的元描述提取的内容,画廊照片似乎是高级自定义字段。

    您应该引入的是来自foreach() 循环的这些设置之一。如果您使用标准 ACF Pro Gallery 字段,alt 属性将包含在数组的第一级。

    alt="<?= esc_attr( $_img['alt'] ); ?>"
    

    否则,您应该能够使用print_r( $_img ); 来找到您需要的属性,例如如果您的$_img 包含'photo' 键,您可能需要使用:

    alt="<?= esc_attr( $_img['photo']['alt'] ); ?>"
    

    文档和函数参考

    【讨论】:

    • 因此 [photo_title] 和 [photo_caption] 是自定义字段,即显示在滑块上的照片标题和照片说明。我正在尝试提取我在 alt 文本字段中为 wordpress 上的每个照片设置输入的 alt 文本。
    • alt 属性应该附加到 $_img 变量的某个地方,如果它已定义(至少,如果您使用 ACF Pro“图库”字段),所以您应该能够使用$_img['alt'];。我已经用它更新了代码,以及一些提示,如果它由于某种原因不存在(使用不同类型的字段等),如何找到 alt
    • 不使用任何自定义字段作为替代文本。它是在所有照片的媒体库中找到的替代字段,因此默认的 wordpress 替代文本字段。
    • 我明白这一点。高级自定义字段是创建 get_field() 函数的插件的名称。如果你做print_r( $_img ),结果是什么?
    • 我得到了一个包含大量数据的数组:[ID]、[link]、[name]、[alt] 等等。但至少它拉入了替代文本。
    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 2012-04-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多