Npcink Blog
切换菜单

有一些主题因为考虑不周,没有画廊功能,点击图片后就到图片地址去了,这样的话,看图片就不方便,那么这次的 WordPress 开发教程就教大家如何给 WordPress 主题添加 fancybox 画廊支持,方便访客查看网站图片。

在footer.php文件的</body>前添加以下代码:

<script src=”https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js”></script>
<link href=”https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.css” rel=”stylesheet”>
<script src=”https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.js”></script>
<script>
var siteTitle = $(“title”).html();//获取标题
$(“.entry-content img”).each(function () {//选取正文的图片
var alt = this.alt;
var src = this.src;
if (!alt) {
$(this).attr(“alt”, siteTitle);
}
//结构重写
$(this).wrap(“<a href='” + src + “‘ class=’fancybox-buttons’ data-fancybox-group=’button’ title='” + alt + “‘></a>”);
});
//fancybox开始 参数参考 http://fancyapps.com/fancybox/3/
$(‘.fancybox-buttons’).fancybox();
</script>

这里只是给一个参考,入门的作用,还有更多精彩的 WordPress 开发操作需要各位开发者自己去发现,关于 Fancybox 可以看看这篇介绍: