无插件实现WordPress文章关键词自动添加链接和wordpress指定关键词手动添加链接代码
无插件实现WordPress与TAG标签内链关联效果 一般都可以使用插件实现,这个是无插件实现TAG内链效果。 缺点就是只能以tga标签来内链,不能指定关键字,如果可以建议使用插件来实现,比如:WP Keyword Link和SEO Smart Link等。
一、文章关键词自动添加链接代码
//连接数量
$match_num_from = 1; //一个关键字少于多少不替换
$match_num_to = 2; //一个关键字最多替换
//连接到WordPress的模块
add_filter('the_content','tag_link',1);
//按长度排序
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
//改变标签关键字
function tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
//连接代码
$cleankeyword = stripslashes($keyword);
$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\"";
$url .= ' target="_blank" class="tag_link"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
//不连接的 代码
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}
把上面的代码脚本,放置到当前WordPress主题中的functions.php文件中,上面对应的描述参数根据需要修改。
二、指定关键词手动添加链接
缺点就是没办法自定义外链数量,如果内链太多对SEO也不是太好,将下面代码加到主题的functions.php文件中:
function replace_text_wps($text){
$replace = array(
'HotNews' => '<a href="http://www.545141.com/" rel="bookmark" title="HotNews Pro主题">HotNews</a>',
'趣玩' => '<a href="http://www.545141.com/" rel="bookmark" title="趣玩博客">趣玩</a>',
'关键词' => '<a href="http://www.545141.com/" rel="bookmark" title="说明">关键词</a>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'replace_text_wps');
第二个方法还可以用来
批量替换Wordpress文章中的文字
如果你在博客的文章中经常加入一些关键词句,但后来准备将这些关键词句替换为其它的内容,手动替换工作量大、而且麻烦。下面的段代码可以非常方便地帮你替换掉这些关键词句。
将下面代码加到主题的functions.php文件中:
function replace_text_wps($text){
$replace = array(
// '关键词' => '替换的关键词'
'wordpress' => '<a href="#">wordpress</a>',
'excerpt' => '<a href="#">excerpt</a>',
'function' => '<a href="#">function</a>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');
参考文章:
无插件实现WordPress程序TAG内链关联效果
http://www.itbulu.com/wp-in-link.html
自动为WordPress关键词添加链接(非插件)
http://zmingcx.com/automatically-add-links-keyword.html
WordPress无插件实现文章关键词自动内链(含标签内链)
http://www.yudouyudou.com/WordPress/212.html
批量替换Wordpress文章中的文字
http://zmingcx.com/automatically-replace-text.html
wordpress文章关键词自动添加链接代码
http://www.511yj.com/wordpress-auto-links.html
wordpress指定关键词手动添加链接
http://www.511yj.com/wordpress-keywords-links.html