非插件方式 实现站内关键词自动添加链接

admin wordpress教程主题插件非插件方式 实现站内关键词自动添加链接已关闭评论字数 1478阅读模式

非插件方式 实现站内关键词自动添加链接

wordpress上实现站内关键词自动添加链接的这类插件比较多,诸如,SEO Smart Links、Keyword Link Plugin等! 对于那些不喜欢用插件的朋友来讲,鲜活优惠码本文介绍的这个代码方式的方法实现站内关键词自动添加链接的方法可能就适合你哦,(有些主题可能不适用)
将下面代码添加到functions.php,具体代码如下:

1
//连接数量

2
$match_num_from = 1; //一个关键字少于多少不替换

3
$match_num_to = 5; //一个关键字最多替换

4
//连接到WordPress的模块

5
add_filter('the_content','tag_link',1);

6
//按长度排序

7
function tag_sort($a, $b){

8
if ( $a->name == $b->name ) return 0;

9
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;

10
}

11
//改变标签关键字

12
function tag_link($content){

13
global $match_num_from,$match_num_to;

14
$posttags = get_the_tags();

15
if ($posttags) {

16
usort($posttags, "tag_sort");

17
foreach($posttags as $tag) {

18
$link = get_tag_link($tag->term_id);

19
$keyword = $tag->name;

20
//连接代码

21
$cleankeyword = stripslashes($keyword);

22
$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword,'$'),__('View all posts in %s'))."\"";

23
$url .= 'target="_blank"';

24
$url .= ">".addcslashes($cleankeyword, '$')."</a>";

25
$limit = rand($match_num_from,$match_num_to);

26
//不连接的代码

27
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case,'$1$2%&&&&&%$4$5', $content);

28
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case,'$1$2%&&&&&%$4$5', $content);

29
$cleankeyword = preg_quote($cleankeyword,'\'');

30
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;

31
$content = preg_replace($regEx,$url,$content,$limit);

32
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);

33
}

34
}

35
return $content;

36
}

以上是非插件方式 实现站内关键词自动添加链接全文 godaddy 优惠码 域名优惠码 

 
admin
  • 本文由 admin 发表于 2013年6月21日 08:03:00
  • Keyword Link Plugin
  • O Smart Links
  • 站内关键词自动添加链接