dedecms模板内容支持TAG标签以及文档关键词

内链锚文本效果
实现教程
1、后台-系统-SQL命令行工具,执行下面语句

INSERT INTO `dede_sysconfig` (`aid`, `varname`, `info`, `groupid`, `type`, `value`) VALUES ('745', 'cfg_replace_links', '文档内链允许最大链接数', '7', 'number', '10'), ('746', 'cfg_replace_key', '是否用关键词做内链', '7', 'bool', 'Y'), ('747', 'cfg_replace_tag', '是否用TAG标签做内链', '7', 'bool', 'Y');


根据自己的需求填写和勾选
2、打开/include/arc.archives.class.php文件
找到

if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y' && !empty($this->Fields['keywords']))


改成

if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y')


再找到

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


把整个函数改成

function GetTags()
{
    global $cfg_cmsurl;
    $this->dsql->SetQuery("SELECT tid FROM `dede_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) ";
    }
    $query = "SELECT * FROM `dede_tagindex` $addsql ORDER BY addtime DESC";
    $this->dsql->SetQuery($query);
    $this->dsql->Execute();
    $result = array();
    while ($row = $this->dsql->GetArray())
    {
        $result[trim($row['tag'])] = $cfg_cmsurl . "/tags.php?/" . urlencode($row['tag']) . "/";
    }
    return $result;
}

function GetKeyWord()
{
    $query = "SELECT * FROM `dede_keywords` WHERE `rpurl` <> '' ORDER BY `rank` DESC ";
    $this->dsql->SetQuery($query);
    $this->dsql->Execute();
    $result = array();
    while ($row = $this->dsql->GetArray())
    {
        $result[trim($row['keyword'])] = trim($row['rpurl']);
    }
    return $result;
}

function ReplaceRes($text, $key, $url)
{
    global $cfg_replace_num;
    $tmp  = $text;
    $tags = $a = array();
    if (preg_match_all("#<a[^>]+>[^<]*</a[^>]*>#su", $tmp, $m))
    {
        $a = $m[0];
        foreach ($m[0] as $k => $z)
        {
            $z   = preg_replace("#\##s", "\#", $z);
            $tmp = preg_replace('#' . $z . '#s', "[_a" . $k . "_]", $tmp, 1);
        }
    };
    if (preg_match_all("#<[^>]+>#s", $tmp, $m))
    {
        $tags = $m[0];
        foreach ($m[0] as $k => $z)
        {
            $z   = preg_replace("#\##s", "\#", $z);
            $tmp = preg_replace('#' . $z . '#s', "[_tag" . $k . "_]", $tmp, 1);
        }
    }
    $key1 = preg_replace("#([\#\(\)\[\]\*])#s", "\\\\$1", $key);
    $tmp  = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)" . $key1 . "(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us", '<a href="' . $url . '" target="_blank"><u>' . $key . '</u></a>', $tmp, $cfg_replace_num);
    if (!empty($a))
    {
        foreach ($a as $n => $at)
        {
            $tmp = str_replace("[_a" . $n . "_]", $at, $tmp);
        }
    }
    if (!empty($tags))
    {
        foreach ($tags as $n => $at)
        {
            $tmp = str_replace("[_tag" . $n . "_]", $at, $tmp);
        }
    }
    return $tmp;
}

function ReplaceKeyword($kw, &$body)
{
    global $cfg_replace_links, $cfg_replace_key, $cfg_replace_tag;
    $cfg_replace_links = isset($cfg_replace_links) ? $cfg_replace_links : 10; // 文档内链允许最大链接数
    $cfg_replace_key   = isset($cfg_replace_key) ? $cfg_replace_key : 'Y'; // 关键词内链(默认开启)
    $cfg_replace_tag   = isset($cfg_replace_tag) ? $cfg_replace_tag : 'N'; // TAG内链(默认不开启)

    if ($cfg_replace_key == 'Y' && $cfg_replace_tag == 'N')
    {
        $arrkey = $this->GetKeyWord();
    }
    elseif ($cfg_replace_key == 'N' && $cfg_replace_tag == 'Y')
    {
        $arrkey = $this->GetTags();
    }
    elseif ($cfg_replace_key == 'Y' && $cfg_replace_tag == 'Y')
    {
        $arrkey = $this->GetKeyWord() + $this->GetTags();
    }
    $keys = array();
    foreach ($arrkey as $k => $url)
    {
        $k = trim($k);
        if (!$k)
        {
            continue;
        }
        if (strpos($body, $k) !== false)
        {
            $keys[$k] = $url;
        }
    }
    $keys_tmp = array_keys($keys);
    usort($keys_tmp, "cmp");
    foreach ($keys_tmp as $i => $key)
    {
        $ki = $i + 2;
        if ($ki > $cfg_replace_links)
        {
            break;
        }
        if (is_array($keys[$key]))
        {
            $url = $keys[$key][rand(0, count($keys[$key]) - 1)];
        }
        else
        {
            $url = $keys[$key];
        }
        $body = $this->ReplaceRes($body, $key, $url);
    }
    return $body;
}


继续找到

function _highlight


在它的上面加入

function cmp($a, $b)
{
    if (mb_strlen($a) == mb_strlen($b))
    {
        return 0;
    }
    return (mb_strlen($a) < mb_strlen($b)) ? 1 : -1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值