Typecho函数实现外链新标签打开
🕙2023-06-21
在主题的functions.php中加入
function parseContent($obj){
$options = Typecho_Widget::widget('Widget_Options');
if(!empty($options->src_add) && !empty($options->cdn_add)){
$obj->content = str_ireplace($options->src_add,$options->cdn_add,$obj->content);
}
$obj->content = preg_replace_callback("/<a href=\"([^\"]*)\">/i", function($matches) use ($options){
$url = $matches[1];
if (strpos($url, $options->siteUrl) === false) {
return "<a href=\"$url\" target=\"_blank\">";
} else {
return "<a href=\"$url\">";
}
}, $obj->content);
echo trim($obj->content);
}
将post.php中的<?php $this->content(); ?>
改为<?php $this->parseContent(); ?>
即可实现内部链接当前标签页打开,外部链接新标签页打开。