如果您想在使用wp editor添加的锚图像包装器上添加一个类,那么可以添加以下代码:这将过滤内容并向图像添加类
<?php
function add_class_to_gallery($content) {
$classes = \'gallery-link\';
// check if there are already a class property assigned to the anchor
if ( preg_match(\'/<a.*? class=".*?"><img/\', $content) ) {
// If there is, simply add the class
$content = preg_replace(\'/(<a.*? class=".*?)(".*?><img)/\', \'$1 \' . $classes . \'$2\', $content);
} else {
// If there is not an existing class, create a class property
$content = preg_replace(\'/(<a.*?)><img/\', \'$1 class="\' . $classes . \'" ><img\', $content);
}
return $content;
}
add_filter(\'the_content\',\'add_class_to_gallery\');
如果您试图向特征图像添加类,则可以使用:
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" class="gallery-link" title="<?php the_title_attribute(); ?>" target="_blank"><img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title_attribute(); ?>"/></a>
<?php endif;?>