要解决AMP WordPress中缺少Alt属性的问题,您可以按照以下步骤进行操作:
functions.php)。// 为AMP图片添加Alt属性
function add_alt_to_amp_images($content) {
global $post;
preg_match_all('/
/', $content, $matches);
if (isset($matches[0])) {
foreach ($matches[0] as $image) {
if (strpos($image, 'class="wp-image') !== false && strpos($image, 'alt=') === false) {
$new_image = str_ireplace('![' . get_post_meta($post->ID, '_wp_attachment_image_alt', true) . ']()
上述代码将为AMP页面中的WordPress图片添加Alt属性。它首先通过正则表达式匹配AMP页面中的所有图片标签,然后检查是否已经设置了alt属性。如果没有设置,则使用get_post_meta函数获取图片的标题作为alt属性,并使用str_ireplace函数将新的alt属性添加到图片标签中。
现在,当你在AMP页面中使用WordPress图片时,它们将自动添加alt属性。请确保为每个图片设置了标题,这样它们的标题将作为alt属性的值。