WordPress 博客中图片显示太小?想点击放大?除去安装 WordPress 现有插件,我们还可以直接添加代码实现。
1. header.php中添加样式
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
2.footer.php中引用js
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
3.function.php中新增代码
/**图片灯箱自动给图片加链接**/
add_filter('the_content', 'fancybox');
function fancybox(content){pattern = array("/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>/i","/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>(.*?)<\/a>/i");
replacement = array('<a1href=23.45 data-fancybox="gallery"><img1src=23.456></a>','<a1href=23.45 data-fancybox="images"6>7</a>');content = preg_replace(pattern,replacement, content);
returncontent;
}
大功告成,可以去试一下啦。