1

我是 wordpress 新手,正在使用自定义主题(http://demo.richwp.com/showcasedark/)。

我想使用我自己的自定义灯箱脚本(每个图像都需要一个自定义类),但我一直试图将该类应用于我的图像库。

例如,每张图片都默认使用以下 html 代码:

a href="_images/anim_reel001.jpg" (后跟img源细节)

我希望它看起来像这样:

a href="_images/anim_reel001.jpg" class="view" (后跟img源细节)

看起来这样做的方法是在我的functions.php文件中创建一个自定义函数,但我对PHP脚本一无所知。此外,我不确定这是否适用于现有图像或仅适用于我上传的新图像/画廊。任何帮助,将不胜感激!

4

1 回答 1

2

如果 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。

于 2013-08-03T12:31:34.957 回答