typecho文章中链接跳转到中转页面
6个月前 > 有的时候想要文章中链接地址点击后跳转到一个中转页面,可以通过下面...
第一步自行百度搜索fancybox进行下载 搞忘提示了:要使用fancybox需要引入jQuery 第二步引入fancybox.css文件
<link rel="stylesheet" type="text/css" href="<?php $this->options->themeUrl('fancybox.css');?>">
第三步引入fancybox.js文件
<script src="<?php $this->options->themeUrl('fancybox.js'); ?>"></script>
通过function.php暴力实现
//Yoniu:取文章首图
function showThumbnail($widget,$imgnum){
$attach = $widget->attachments(1)->attachment;
$pattern = '|<img[^>]+src=\"([^>\"]+)\"?[^>]*>|is';
$patternMD = '/\!\[.*?\]\((http(s)?:\/\/.*?(jpeg|gif|jpg|png))/i';
$patternMDfoot = '/\[.*?\]:\s*(http(s)?:\/\/.*?(jpeg|gif|jpg|png))/i';
//如果文章内有插图,则调用插图
if (preg_match_all($pattern, $widget->content, $thumbUrl)) {
echo '<a href="'.$thumbUrl[1][$imgnum].'" data-fancybox="gallery"><img src="'.$thumbUrl[1][$imgnum].'"></a>';
}
//没有就调用第一个图片附件
else if ($attach && $attach->isImage) {
echo '<a href="'.$attach->url.'" data-fancybox="gallery"><img src="'.$attach->url.'"></a>';
}
//如果是内联式markdown格式的图片
else if (preg_match_all($patternMD, $widget->content, $thumbUrl)) {
echo '<a href="'.$thumbUrl[1][$imgnum].'" data-fancybox="gallery"><img src="'.$thumbUrl[1][$imgnum].'"></a>';
}
//如果是脚注式markdown格式的图片
else if (preg_match_all($patternMDfoot, $widget->content, $thumbUrl)) {
echo '<a href="'.$thumbUrl[1][$imgnum].'" data-fancybox="gallery"><img src="'.$thumbUrl[1][$imgnum].'"></a>';
}
//如果真的没有图片,就调用一张随机图片
else{
echo '';
}
}
图片调用方法(请注意传入的值,$this并不是万能的,第一参数必须为对象类型,第二个参数为要调用的数组的键)
<?php showThumbnail($this,0); ?>#免责声明#

6个月前 > 有的时候想要文章中链接地址点击后跳转到一个中转页面,可以通过下面...

6个月前 进行更新微博客程序的时候,准备重新修改一下评论插件的回复表单定位问...

5个月前 PHP超全局变量不同使用方法的差异有多大?,列如下面的代码案例,各位网...

5个月前 第一步需要添加两个css样式,一个为有内边距或者外边距的样式,一个为什...

6个月前 以前都没有用过这个功能,近期在搞一个东东遇到了这个问题,怎么实现分...

4个月前 这款js弹窗气泡代码非常简约,能够胜任日常弹窗业务。 ![火狐截图_2025-09...