欢迎来到站长天空!

WordPress

当前位置: 主页 > CMS教程 > WordPress

不用插件WordPress怎样实现自动设置tag标签自动内链?

时间:2025-11-11 15:08:21|栏目:WordPress|点击:

在WordPress使用自动设置tag标签的时候有很多方法,平时最常用的就是靠插件来实现功能,最常用的插件又是WP keyword Link Plugin插件不用插件WordPress怎样实现自动设置tag标签自动内链?,可能是好久没有更新了貌似找不到了,所以考虑用一些类似的插件例如Auto Tag Links、Keywords to Links Converter等这些都可以。

不用插件WordPress怎样实现自动设置tag标签自动内链?

本着少用插件,能不用插件就不用的策略,所以准备还是无插件实现。

//自动TAG转内链
$match_num_from = 2; // 一个TAG标签出现几次才加链接
$match_num_to = 1; // 同一个标签加几次链接
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, '$')."</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;
}

将代码添加到当前主题Functions.php文件中。如果有冲突出现问题,我们需要检查是不是不兼容。检查不出来的话,我们只能使用插件实现。


上一篇:国外WordPress精美主题如何DIY汉化成中文版呢?

栏    目:WordPress

下一篇:10个改善WordPress主题的小提示

本文标题:不用插件WordPress怎样实现自动设置tag标签自动内链?

本文地址:https://zz.feitang.co/CMSjiaocheng/25955.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号