很多时候在用WordPress发布文章的时候,为了更好的SEO,因此都会加一些内链,但是每次都要手动添加内链很麻烦。今天虾皮路就介绍一下这个强大的WordPress插件-Simple Tags,可以自动添加内链tag关键词,完美支持中文,不像有的插件对中文支持不完美。
1、插件介绍
Extended Tagging for WordPress : Terms suggestion, Mass Edit Terms, Auto link Terms, Ajax Autocompletion, Click Terms, Advanced manage terms, etc.
This is THE perfect tool to manage perfectly your WP terms for any taxonomy
It was written with this philosophy : best performances, more secured and brings a lot of new functions
This plugin is developped on latest WordPress version, with the constant WP_DEBUG to TRUE.
Simple Tags是一款强大的标签插件,有相关日志、相关标签、管理标签云、自动匹配日志关键词、自动添加关键词内链等功能。
2、插件下载
下载地址:https://wordpress.org/plugins/simple-tags/
如果无法访问以上地址可以从本地下载:
下载地址:https://shopee6.lanzous.com/iyjFfdv5o5i
或者也可以直接在WordPress后台在线安装,直接搜索Simple Tags
安装启用即可。
3、simple-tags使用教程
安装之后,进入 Simple Tags 设置界面,如图所示。因为插件是英文版的,不过可以直接用谷歌浏览器的翻译成中文,对照着使用。
首先,我们要勾选 Auto links tags 这个功能,这也是我们安装这个插件的主要目的。
其次,我们点到 Auto link 这个设置页,然后可以修改一下最后一个选项里的内容,简单汉化一下,比如改成我这样的“查看 %s 相关文章”。
其他的设置的话,大家就根据自己的需求进行修改吧。设置完成后,清除自己的站点缓存,下次访问文章的时候自动添加了tag关键词内链。
4、代码方法
如果不想安装以上插件的话,可以使用纯代码的方法,代码如下:
/**
* WordPress 自动为文章标签添加该标签的链接
* https://www.xiapilu.com/web/wordpress-plugin/simple-tags.html
*/
$match_num_from = 1; // 一个标签在文章中出现少于多少次不添加链接
$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文件里,记得是最后一个?>之前,然后保存即可。
评论前必须登录!
立即登录 注册