惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

Evan.Plus

ZUI.RE:一个域名的非典型归宿,以及我对”看新闻”这件事的重新理解 – Evan.Plus PIXPRO主题:移动端CSS优化 – Evan.Plus macOS 一键安装 Ropy:从终端到剪贴板历史,全程 30 秒 – Evan.Plus PIXPRO主题:pix-down.php报错修复 – Evan.Plus PIXPRO主题封面魔改教程 – Evan.Plus PIXPRO主题基础安装教程(持续更新) – Evan.Plus PIXPRO主题安装教程:XLoader PHP扩展的安装与配置 – Evan.Plus 34个顶级HTML的PPT设计模版 – Evan.Plus 世界,您好! – Evan.Plus
PIXPRO 主题:图片灯箱绑定教程(Fancybox 版) – Evan.Plus
Evan · 2026-06-09 · via Evan.Plus

适用场景

你的主题已经引入了 Fancybox(版本 3/4/6 均可),但文章图片没有被正确绑定,点击无反应或跳转错误。

核心问题

  1. 文章图片没有 data-fancybox 属性,Fancybox 无法识别
  2. 文章缩略图链接到文章页,误触灯箱会阻断正常跳转
  3. 外部图片和本地图片需要同时兼顾

解决方案

把以下代码放进主题的 footer.php,放在 </body> 标签之前:

php

<script>
document.addEventListener('DOMContentLoaded', function() {
    // 定义可能包含图片的选择器(覆盖文章页 + 首页列表)
    var selectors = [
        '.post-content img',      // 文章正文
        '.entry-content img',     // 文章正文(备选)
        'article img',            // article 标签内
        '.post-item img',         // 首页文章列表
        '.post-feature img',      // 文章特色图
        '.hentry img',            // 文章条目
        '.post-thumbnail img',    // 缩略图
        '.wp-block-image img'     // Gutenberg 图片块
    ];
    
    // 合并去重
    var allImages = [];
    selectors.forEach(function(sel) {
        document.querySelectorAll(sel).forEach(function(img) {
            if (allImages.indexOf(img) === -1) {
                allImages.push(img);
            }
        });
    });
    
    allImages.forEach(function(img) {
        var src = img.getAttribute('src');
        if (!src) return;
        
        var parent = img.parentElement;
        
        // 情况一:图片已经在 a 标签里
        if (parent && parent.tagName === 'A') {
            var href = parent.getAttribute('href') || '';
            
            // 关键判断:a 的 href 是否指向图片文件
            var isImageLink = /\.(jpg|jpeg|png|gif|webp|svg|bmp|avif)(\?.*)?$/i.test(href);
            
            if (isImageLink) {
                // 是图片链接 → 绑定灯箱
                parent.setAttribute('data-fancybox', 'gallery');
                parent.setAttribute('data-caption', img.getAttribute('alt') || '');
            }
            // 不是图片链接(如文章页URL)→ 保持原跳转,不做任何处理
            
            return;
        }
        
        // 情况二:图片没有 a 包裹 → 自动创建 a 指向图片本身
        var a = document.createElement('a');
        a.setAttribute('href', src);
        a.setAttribute('data-fancybox', 'gallery');
        a.setAttribute('data-caption', img.getAttribute('alt') || '');
        img.parentNode.insertBefore(a, img);
        a.appendChild(img);
    });
    
    // Fancybox 绑定
    if (typeof Fancybox !== 'undefined') {
        Fancybox.bind('[data-fancybox="gallery"]');
    }
});
</script>

效果演示