【问题标题】:Wordpress theme not displaying ALT tagsWordpress 主题不显示 ALT 标签
【发布时间】:2016-06-01 16:29:29
【问题描述】:

我正在尝试在网站上显示图像时为图像添加 alt 属性,但到目前为止还没有成功。

非常感谢,这是我正在尝试的代码:

<?php $featured = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), '1440_1712'); ?>
<?php $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true); ?>
<div class='img-half-wrap' style='background-image: url("<?php echo $featured[0]; ?>");' alt='<?php echo $alt; ?>'>

我尝试将 $post->ID 更改为 $featured,但没有成功。 非常感谢任何帮助。

谢谢

【问题讨论】:

  • 如果您在没有可选参数的情况下测试get_post_meta($post-&gt;ID),您会看到什么结果?
  • 感谢您的及时回复。我看到了 alt="Array"。

标签: php wordpress image alt


【解决方案1】:

您在错误的对象上使用了get_post_meta:您想要的是来自媒体_wp_attachment_image_alt 元数据,而不是来自张贴

所以你应该使用这个:

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

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多