教程前言:
我们平时编辑更新文章时,系统会自动检查文章时钟中是否有标签词,然后将其加入到链接优化内链的结构中。
只要文章中的标签足够多,就可以通过代码添加到标签链优化中,这样会大大降低编辑优化文章的工作效率。
当然还有一键生成插件,不推荐,大家都懂。
自动内链标签代码:
添加在主题目录下functions.php文件最后一个?>即可
/* 自动为文章内的标签添加内链 */
$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 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," target="”_blank”‘;" rel="noopener">”.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;
}
add_filter(‘the_content’,’tag_link’,1);
教程截图演示:
转载请注明:汇站网 » WordPress使用代码给文章自动增加内链优化
汇站网 WordPress教程 WordPress使用代码给文章自动增加内链优化 https://www.huizhanii.com/29422.html


常见问题
相关文章
猜你喜欢
- WordPress禁用自动生成缩略图和多尺寸图片的方法(经过亲测可行) 2023-09-27
- WordPress网站速度优化技巧(珍藏代码版)让您的网站速度提升100% 2023-09-27
- 在WordPress中添加复制版权提示 2023-09-24
- 解决 WordPress 启用 CDN 后无法获取用户真实 IP 的方法 2023-09-24
- 解决 Windows 无法使用代理的问题的可能方法 2023-09-24
- WordPress利用jQuery代码绕过防盗链限制 2023-09-23
- WordPress 如何改变JPEG图片的压缩质量 2023-09-22
- 禁止WordPress订阅用户登陆后访问后台和跳转到指定页面 2023-09-22
- 纯代码自动为WordPress文章添加已使用过的标签 2023-09-21
- 纯代码生成WordPress网站地图使用XML格式创建一个sitemap.xml 2023-09-21