试试这个。已通过$attachment->ID
到get_post_meta
检索每个图像的alt标记
add_filter(\'wp_get_attachment_link\', \'galleryWIthClass\');
function galleryWithClass($src) {
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => null,
\'post_status\' => null,
\'post_parent\' => get_the_ID()
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$alt = get_post_meta($attachment->ID, \'_wp_attachment_image_alt\', true);
$html= str_replace(\'<img\', \'<img class="\'. $alt.\'" \', $src);
return $html;
}
}
}