其实可以发布文章的时候,去添加外链,但是个人觉得很麻烦,而且文章数量多的话,维护起来更加麻烦了,添加内链可以引导蜘蛛爬行走向,对网站:首页-栏目-文章的分层权重是很有帮助的
如果不喜欢插件,可以按教材实现到主题里面$Keywordspress = new Typecho_Widget_Helper_Form_Element_Textarea('Keywordspress', NULL, NULL, _t('关键字内链'), _t('每行1组以"关键词|(半角竖线)链接"形式填写)'));
$form->addInput($Keywordspress);
/**
* 文章内容替换为内链
*/
function get_glo_keywords($content)
{
$settings = Helper::options()->Keywordspress;
$keywords_list = array();
if (strpos($settings,'|')) {
//解析关键词数组
$kwsets = array_filter(preg_split("/(\r|\n|\r\n)/",$settings));
foreach ($kwsets as $kwset) {
$keywords_list[] = explode('|',$kwset);
}
}
ksort($keywords_list); //对关键词排序,短词排在前面
if($keywords_list){
$readnum = 0;
$i = 0;
$j = 1;
foreach ($keywords_list as $key => $val) {
$title = $val[$i];
$len = strlen($title);
$str = '@'.$title.'';
$str_index = mb_strpos($content, $title);
$content = preg_replace('/(?!]*)'.$title.'(?![^)/',$str,$content,1);
if(is_numeric($str_index)){
$readnum += 1;
//$content = substr_replace($content,$str,$str_index,$len);
//$content = $this->str_replace_limit($title,$str,$content,$this->limit);
}
if($readnum == 8) {
return $content; //匹配到8个关键词就退出
$i += 2;
$j += 2;
}
}
}
return $content;
}
主要是涉及到3个问题,获取到主题里面的字符串,解析到数值里面,然后循环赋值到正则替换文章内容,则实现了