WordPress文章关键词自动添加内链链接
在主题根目录下的functions.php
文件的<?php
下添加以下代码
-
/*
-
*Wordpress文章关键词自动添加内链链接代码
-
*/
-
//连接数量
-
$match_num_from = 1; //一篇文章中同一个关键字少于多少不锚文本(这个直接填1就好了)
-
$match_num_to = 1; //一篇文章中同一个关键字最多出现多少次锚文本(建议不超过1次)
-
//连接到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, '$'),__('查看所有文章关于 %s'))."\"";
-
$url .= 'target="_blank"';
-
$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;
-
}
评论:
日历
分类
最新评论
- 鸟叔
拿走用了,感谢 - 雷达探币
旧时光是个哑巴,唱不... - lijiayan
@林林:是的~ - 雷达探币
妾似琵琶斜入抱,任君... - 林林
如果网站是使用htt... - 林林
能换个友情链接吗? ... - 马内
感谢博主的分享,支持... - aussiegabmbling
论坛过来看看博主 - aussie_gabmbling
關於這個主題的好網站 - 大卖家
很好啊,顶一下
热门日志
随机日志
存档
- 2021年12月(1)
- 2021年11月(1)
- 2021年10月(1)
- 2021年8月(2)
- 2021年7月(1)
- 2021年6月(1)
- 2021年5月(1)
- 2021年4月(2)
- 2019年12月(1)
- 2019年5月(1)
- 2018年12月(1)
- 2018年9月(2)
- 2018年7月(2)
- 2018年6月(2)
- 2018年5月(1)
- 2018年4月(2)
- 2018年3月(2)
- 2018年2月(5)
- 2017年9月(1)
- 2017年8月(1)
- 2017年4月(1)
- 2016年12月(5)
- 2016年8月(2)
- 2016年3月(2)
- 2016年1月(1)
- 2015年11月(1)
- 2015年9月(2)
- 2015年8月(2)
- 2015年6月(2)
- 2015年5月(2)
- 2015年4月(2)
- 2015年3月(2)
- 2015年1月(1)
- 2014年11月(6)
- 2014年10月(2)
- 2014年9月(2)
- 2014年8月(6)
- 2014年7月(3)
- 2014年6月(10)
- 2014年5月(3)
- 2014年4月(2)
- 2014年3月(9)
- 2014年1月(1)
- 2013年12月(9)
- 2013年11月(11)
- 2013年10月(7)
- 2013年9月(18)
- 2013年8月(8)
- 2013年7月(15)
- 2013年6月(17)
- 2013年5月(17)
- 2013年4月(10)
- 2013年3月(17)
- 2013年2月(20)
- 2013年1月(37)
- 2012年12月(12)
- 2008年12月(1)
2022-05-29 09:14