步骤 1:引入 Fancybox 文件
修改主题的 header.php 文件,在 <head> 标签内添加以下代码:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
修改 footer.php 文件,在 </body> 标签前添加:
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
步骤 2:自动为图片添加链接
在主题的 functions.php 文件中添加以下代码:
add_filter('the_content', 'fancybox');
function fancybox($content) {
$pattern = array(
"/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\" )(.*?)>/i"
);
$replacement = '<a href="$3.$4" data-fancybox="gallery"><img$1src="$3.$4"$6></a>';
return preg_replace($pattern, $replacement, $content);
}
最后随便编辑一篇带图片的文章检验一下有没有生效。

