织梦dede文章关键词自动内链方法

织梦dede文章关键词自动内链方法,在网上查了很久,办法也很多,按照设置了内链却没有出来,试过了很多办法终于把内链设置好了,并加以优化,链接关键词加粗,在此分享给大家。最终效果如下图

织梦dede文章关键词自动内链方法
下面就是具体步骤:

1、核心》批量维护》文档关键词维护,新增关键词和链接,点击确定
织梦dede文章关键词自动内链方法

2、系统》系统基本参数》性能选项》使用关键词关联文章选“是”
 
织梦dede文章关键词自动内链方法

3、系统》系统基本参数》其他选项》档内容同一关键词替换次数(0为全部替换):填“1”
 
织梦dede文章关键词自动内链方法
 
4、根目录找到include/arc.archives.class.php,ctrl+f:function ReplaceKeyword($kw,&$body)  找到

    /**
     * 高亮问题修正, 排除alt title <a></a>直接的字符替换
     *
     * @param string $kw
     * @param string $body
     * @return string
     */
    function ReplaceKeyword($kw,&$body)
    {
        global $cfg_cmspath;
        $maxkey = 5;
        $kws = explode(",",trim($kw));    //以分好为间隔符
        $i=0;
        $karr = $kaarr = $GLOBALS['replaced'] = array();

        //暂时屏蔽超链接
        $body = preg_replace("#(<a(.*))(>)(.*)(<)(/a>)#isU", '-]--[-', $body);

        /*
        foreach($kws as $k)
        {
            $k = trim($k);
            if($k!="")
            {
                if($i > $maxkey)
                {
                    break;
                }
                $myrow = $this->dsql->GetOne("SELECT * FROM dede_keywords WHERE keyword='$k' AND rpurl<>'' ");
                if(is_array($myrow))
                {
                    $karr[] = $k;
                    $GLOBALS['replaced'][$k] = 0;
                    $kaarr[] = "<a href='{$myrow['rpurl']}'><u>$k</u></a>";
                }
                $i++;
            }
        }
        */
		$dsql = new DedeSql(false);$query="select * from dede_keywords where rpurl<>'' ";$dsql->SetQuery($query);$dsql->Execute();while($row = $dsql->GetArray()){$key = trim($row['keyword']);$rul=trim($row['rpurl']);if($rul!="")if(!in_array($key, $kw))if(!in_array($kw,$key))if(substr_count($body,$key)<20){$karr[] = $key;$kaarr[] = "$key";}}
		
		
        $query = "SELECT * FROM dede_keywords WHERE rpurl<>'' ORDER BY rank DESC";
        $this->dsql->SetQuery($query);
        $this->dsql->Execute();
        while($row = $this->dsql->GetArray())
        {
            $key = trim($row['keyword']);
            $key_url=trim($row['rpurl']);
            $karr[] = $key;
            $kaarr[] = "<a href='$key_url' target='_blank'><u>$key</u></a>";
        }

        // 这里可能会有错误
        if (version_compare(PHP_VERSION, '5.5.0', '>='))
        {
            $body = @preg_replace_callback("#(^|>)([^<]+)(?=<|$)#sU", "_highlight('', $karr, $kaarr, '')", $body);
        } else {
            $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('', $karr, $kaarr, '')", $body);
        }

        //恢复超链接
        $body = preg_replace("#(<a(.*))-]-(.*)-[-(/a>)#isU", '><', $body);
        return $body;
    }


}//End Archives

//高亮专用, 替换多次是可能不能达到最多次
function _highlight($string, $words, $result, $pre)
{
    global $cfg_replace_num;
    if (version_compare(PHP_VERSION, '5.5.0', '>='))
    {
        $string = $string[0];
        $pre = $pre[0];
    }
    $string = str_replace('"', '"', $string);
    if($cfg_replace_num > 0)
    {
        foreach ($words as $key => $word)
        {
            if($GLOBALS['replaced'][$word] == 1)
            {
                continue;
            }
            $string = preg_replace("#".preg_quote($word)."#", $result[$key], $string, $cfg_replace_num);
            if(strpos($string, $word) !== FALSE)
            {
                $GLOBALS['replaced'][$word] = 1;
            }
        }
    }
    else
    {
        $string = str_replace($words, $result, $string);
    }
    return $pre.$string;
}

全部替换为下面的代码:

    /**
     * 高亮问题修正, 排除alt title <a></a>直接的字符替换
     *
     * @param string $kw
     * @param string $body
     * @return string
     */
    function ReplaceKeyword($kw,&$body){  
	global $cfg_cmspath;  
	$maxkey = 5;  
	$kws = explode(",",trim($kw)); //以分好为间隔符  
	$i=0;  
	$karr = $kaarr = $GLOBALS['replaced'] = array();  
  
	//暂时屏蔽超链接  
	$body = preg_replace("/(<a(.*))(>)(.*)(<)(/a>)/isU", '-]--[-', $body);  
	global $dsql;  
	$query="SELECT * FROM dede_keywords WHERE rpurl<>'' and sta=1 ORDER BY length(keyword)desc";  
	$dsql->SetQuery($query);  
	$dsql->Execute();  
	while($row = $dsql->GetArray()){  
	$key = trim($row['keyword']);  
	$key_url=trim($row['rpurl']);  
	$karr[] = $key;  
	$kaarr[] = "<strong><a href='$key_url' target='_blank' title='$key'>$key</a></strong>";
	}  
	foreach ($karr as $key => $word){  
	$body = preg_replace("/(^|>)([^<]+)(?=<|$)/sUe", "_highlight('', $karr[$key], $kaarr[$key], '')", $body);  
	//echo $body."<br/>";  
	//恢复超链接  
	$body = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU", '><', $body);  
	//暂时屏蔽超链接  
	$body = preg_replace("/(<a(.*))(>)(.*)(<)(/a>)/isU", '-]--[-', $body);  
	}  
	//恢复超链接  
	$body = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU", '><', $body);  
	return $body;  
	}  
	}//End Archives  
	//高亮专用, 替换多次是可能不能达到最多次  
	function _highlight($string, $words, $result, $pre){  
	global $cfg_replace_num;  
	$string = str_replace('"', '"', $string);  
	if($GLOBALS['replaced'][$words] == 1){  
	return $pre.$string;  
	}  
	if($cfg_replace_num > 0){  
	$string = preg_replace("/".preg_quote($words)."/", $result, $string, $cfg_replace_num);  
	if(strpos($string, $words) !== false){  
	$GLOBALS['replaced'][$words] = 1;  
	//echo $words;  
	}  
	}  
	else{  
	$string = str_replace($words, $result, $string);  
	}  
	return $pre.$string;  
	}  

5、更新生成全部文档,至此织梦dede文章关键词自动内链设置完成

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.cgq0628.com/cms/11.html

说点什么吧

说点什么吧
  • 全部评论(0
    还没有评论,快来抢沙发吧!