帝国CMS:Tag自动关联内容,并同步内容锚文本教程

2025-05-29 0 105

 

//TAG自动关联当前关键词
function tihuanmuban($id) {
global $empire, $dbtbpre, $public_r, $class_r;

// 获取新闻的基本信息和内容
$tagr = $empire->fetch1("select id, classid from {$dbtbpre}ecms_news where id='$id' limit 1");
$f = $empire->fetch1("select newstext from {$dbtbpre}ecms_news_data_1 where id='$id' limit 1");
$newstext = $f['newstext'];

// 获取新闻的标签ID
$sql = $empire->query("select tagid from {$dbtbpre}enewstagsdata where id=".$tagr['id']." and classid=".$tagr['classid']."");

// 用于存储已处理的标签ID,确保不重复替换
$processedTagIds = [];

while ($td = $empire->fetch($sql)) {
$tr = $empire->fetch1("select * from {$dbtbpre}enewstags where tagid=".$td['tagid']." limit 1");
if ($tr['tagid'] && !in_array($tr['tagid'], $processedTagIds)) {
$processedTagIds[] = $tr['tagid']; // 标记为已处理

$tagurl = dp_itagLink($td['tagid']);

// 使用回调函数和正则表达式实现只替换一次
$newstext = preg_replace_callback(
'/('.preg_quote($tr['tagname'], '/').')(?!([^<]*>)|([^<]*<\/a>))/i',
function ($matches) use ($tagurl) {
return '<a href="'.$tagurl.'" rel="external nofollow"  target="_blank" style="color: #ff6a00;">'.$matches[1].'</a>';
},
$newstext,
1 // 这里的1表示只替换一次
);
}
}

return $newstext;
}
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

帝国CMS模板网 帝国CMS教程 帝国CMS:Tag自动关联内容,并同步内容锚文本教程 https://www.daixiao360.com/5393.html

相关文章

发表评论
暂无评论