首页 > 解决方案 > 如何访问wordpress中的图像属性?

问题描述

我正在尝试从 WordPress 文件中访问图像的 alt 属性,但不知道任何有助于此的功能。我想设置这个函数的最后一个属性。alt 文本将在上传图像时设置,然后此函数应获取 alt 文本并将其显示在<img> tag.

<?php echo get_the_post_thumbnail( $post->ID, 'large', 'alt=' ); ?>

标签: wordpress

解决方案


使用此代码

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

推荐阅读