wordpress - 如何访问wordpress中的图像属性?
问题描述
我正在尝试从 WordPress 文件中访问图像的 alt 属性,但不知道任何有助于此的功能。我想设置这个函数的最后一个属性。alt 文本将在上传图像时设置,然后此函数应获取 alt 文本并将其显示在<img> tag
.
<?php echo get_the_post_thumbnail( $post->ID, 'large', 'alt=' ); ?>
解决方案
使用此代码
<?php $imgAlt = get_post_meta($imgID,'_wp_attachment_image_alt', true); ?>
获取特色图像源和替代文本的完整代码
<?php
$imgID = get_post_thumbnail_id($post->ID);
$img = wp_get_attachment_image_src($imgID,'full', false, '');
$imgAlt = get_post_meta($imgID,'_wp_attachment_image_alt', true);
?>
<img src="<?php echo $img[0]; ?>"alt="<?php echo $imgAlt; ?>" />
推荐阅读
- java - 我想在滚动时更改应用程序工具栏的颜色
- javascript - Is there a way to replace today date with hours?
- hive - HIVE:插入似乎正在覆盖现有表
- r - 如何在gganimate中一次出现一个栏
- ruby - 重新分配的哈希更改了原始哈希
- javascript - 在 Node 中使用 Jest 模拟使用链式函数调用的节点模块
- documentum - Documentum D2 - 未找到类类型接口 com.emc.d2fs.dctm.content.ID2Content
- java - Selenium Java - 如何通过 Selenium 从 html 中提取数字
- dialogflow-es - 如何使用 Keycloak 和 Actions on Google 进行登录?
- angular - 在管道中使用过滤器和映射来减少集合