# dedecms当前文档内容如何自动关联tag标签做内链

*Published:* 2023-12-13
*Author:* 客服001

#### 温馨提醒

如果文章内容或图片资源失效，请留言反馈，我们会及时处理，谢谢

本文最后更新于2023年12月13日，已超过 180天没有更新



实现教程如下：

1、后台-系统-核心设置-关键字替换，选择【是】

2、后台-系统-其他选项-关键词替换次数，填【1】或者【0】

> 1：表示文档内容里有多个关键词，只让1个是[内链](https://www.xarjtc.com/tag/%e5%86%85%e9%93%be "查看内链此标签更多文章")
> 
> 0：表示文档内容里有多个关键词，都是内链

根据自己的需要填

3、打开 /include/arc.archives.class.php 找到，大概在1187行至1241行，整个函数

```
function ReplaceKeyword($kw,&$body)
{
...中间代码省略
}
```

改为：

```
function ReplaceKeyword($kw,&$body)
{
global $cfg_replace_num;
$search = "/(alt\s*=\s*|title\s*=\s*|src\s*=\s*)[\"|\'](.+?)[\"|\']/is";
$body = preg_replace_callback($search, array('Archives', '_base64_encode'), $body);
$addsql = '';
if(isset($this->Fields['<a class="tag_link" href="https://www.xarjtc.com/tag/tag" target="_blank" title="查看tag此标签更多文章"><abbr title="">tag</abbr></a>s']) && !empty($this->Fields['aid']))
{
$this->dsql->SetQuery("SELECT tid FROM `#@__taglist` WHERE aid = '{$this->Fields['aid']}' ");
$this->dsql->Execute();
$ids = '';
while($row = $this->dsql->GetArray())
{
$ids .= ( $ids=='' ? $row['tid'] : ','.$row['tid'] );
}
if($ids != '')
{
$addsql = " WHERE id IN($ids) ";
}
if($addsql=='') return $body;
}
$query = "SELECT * FROM `#@__tagindex` $addsql ORDER BY addtime DESC";
$this->dsql->SetQuery($query);
$this->dsql->Execute();
$linkdatas = array();
while($row = $this->dsql->GetArray())
{
$row['keyword'] = $row['tag'];
$row['rpurl'] = $cfg_cmsurl."/tags.php?/".urlencode($row['tag'])."/";
$linkdatas[] = $row;
}
if($linkdatas) {
$word = $replacement = array();
foreach($linkdatas as $v) {
$word1[] = '/(?!(<a.*?))' . preg_quote($v['keyword'], '/') . '(?!.*<\/a>)/s';
$word2[] = $v['keyword'];
$replacement[] = '<a href="'.$v['rpurl'].'" target="_blank">'.$v['keyword'].'</a>';
}
if($cfg_replace_num) {
$body = preg_replace($word1, $replacement, $body, $cfg_replace_num);
} else {
$body = str_replace($word2, $replacement, $body);
}
}
$body = preg_replace_callback($search, array('Archives', '_base64_decode'), $body);
return $body;
}
function _base64_encode($matches) {
return $matches[1]."\"".base64_encode($matches[2])."\"";
}
function _base64_decode($matches) {
return $matches[1]."\"".base64_decode($matches[2])."\"";
}
```

4、完成