在Wordpress滑块中获取图像alt文本

在Wordpress滑块中获取图像alt文本,wordpress,alt,Wordpress,Alt,我们的网站在头版使用主题的slider.php。我希望该滑块中显示的图像使用post meta中的alt文本(在WP媒体库中编辑) 我试图创建一个变量($alt),并在调用img时让该变量回显,但结果是空的。下面是slider.php的相关部分: $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true); <img src="<?php echo $thumbnail[0]; ?>" alt

我们的网站在头版使用主题的slider.php。我希望该滑块中显示的图像使用post meta中的alt文本(在WP媒体库中编辑)

我试图创建一个变量($alt),并在调用img时让该变量回显,但结果是空的。下面是slider.php的相关部分:

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



<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt[0]; ?>" />
    </a>

<?php else: ?>
$alt=get\u post\u meta($post->ID,'.\u wp\u attachment\u image\u alt',true);
“alt=”“/>
`
知道我做错了什么吗?

您已经将第三个参数设置为
true
,这意味着函数
get\u post\u meta
返回一个字符串

因此,以下方法应该起作用

<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt; ?>" />
“alt=”“/>

好的-我已经解决了这个问题。上面的代码试图从图像的缩略图获取post meta,据我所知,缩略图图像没有元数据


我刚刚创建了一个自定义字段,现在滑块循环调用了该字段。

我使用了$alt变量的回音,但仍然不起作用。老鼠,啊!很高兴你解决了这个问题-记住你可以接受自己的答案:)