侧边栏壁纸
博主头像
Wood Chen博主等级

独唱独酬,独行独坐还独卧

  • 累计撰写 207 篇文章
  • 累计创建 126 个标签
  • 累计收到 6 条评论

目 录CONTENT

文章目录

wordpress自动给关键词加上tag内链

wood
2022-01-06 / 0 评论 / 0 点赞 / 25 阅读 / 1449 字
//WordPress 文章关键词自动内链
//自动TAG转内链

$match_num_from = 1; // 一个TAG标签出现几次才加链接

$match_num_to = 2; // 同一个标签加几次链接

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"';

$url .= ">".addcslashes($cleankeyword, '$')."";

$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 . ')(?!(([^<>]*?)>)|([^>]*?))\'s' . $case;

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

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

}

}

return $content;

}
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区