如果 jQuery 是一个选项...您可以定位所有画廊并确保将该类添加到锚点。你可以在这里看到这个:
查看小提琴结果中占位符图像的源代码,您可以看到,即使在标记中<a>
它现在没有类。
这适用于 3 块代码来模仿 WordPress 库输出的内容。
HTML
<div id="gallery-1" class="gallery">
<dl class="gallery-item">
<dt class="gallery-icon">
<a href="#">
<img width="75" height="75" src="http://dummyimage.com/75x75/000/fff.jpg" class="attachment-thumbnail" />
</a>
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon">
<a href="#">
<img width="75" height="75" src="http://dummyimage.com/75x75/000/fff.jpg" class="attachment-thumbnail" />
</a>
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon">
<a href="#">
<img width="75" height="75" src="http://dummyimage.com/75x75/000/fff.jpg" class="attachment-thumbnail" />
</a>
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon">
<a href="#">
<img width="75" height="75" src="http://dummyimage.com/75x75/000/fff.jpg" class="attachment-thumbnail" />
</a>
</dt>
</dl>
CSS
#gallery-1 {
margin: auto;
}
#gallery-1 .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: 25%; }
#gallery-1 img {
border: 2px solid #cfcfcf;
}
#gallery-1 .gallery-caption {
margin-left: 0;
}
jQuery
$(document).ready(function(){
$('div.gallery').each(function(i) {
$('.attachment-thumbnail').parent().addClass('view');
});
});
好东西发生在 jQuery 代码块中,因为您可以看到,当文档准备好时,我们通过选择它们来遍历所有画廊,div.gallery
然后我们循环通过它们中的每一个以找到.attachment-thumbnail
提供给<img>
拇指为 WordPress 画廊,我们的目标是<img>
恰好是<a>
标签的父级,并添加您想要调用的类.view
。
值得注意的是,要使此解决方案适合您,您需要在文件中包含 jQuery,否则这将不起作用。您可能已经包含了 jQuery。