有一些主題因為考慮不周,沒有畫廊功能,點擊圖片后就到圖片地址去了,這樣的話,看圖片就不方便,那么這次的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可以看看這篇介紹: