WordPress设置文章自动添加标签内链

以前我看见像站壳网,痴痴的网站这些大佬的网站,他们的文章好像有一些标签内链,点进去可以跳转,我和疑惑,到底这个怎么弄?然后昨天在栗子网站上找到了这样的教程,一起来看看WordPress设置文章自动添加标签内链方法吧,大佬的网站都在用,咱也来试试吧。

我们知道网站不是只做外链行,还需要做好内链的建设,以前我为了做好内链,我一般都是每篇文章添加一两个其他文章的链接,用这种方法来添加内链,后来觉得太麻烦,而且感觉费劲,然后不这么弄了,直到我看见很多大佬的网站都是有标签跳转内链的功能,我一直很疑惑,这玩意到底怎么弄?

这不昨天晚上从栗子网站上找到了方法,我赶紧弄上了,如果说网站里面添加标签没有什么用,那么,文章内部添加标签内链是肯定有用的,那么多大佬的网站都这样搞,所以我也跟着一块搞上吧。

方法:打开主题编辑器

WordPress设置文章自动添加标签内链插图

点击主题文件编辑器

WordPress设置文章自动添加标签内链插图1

进入functions.php文件,然后在里面添加下面这个代码

//自动TAG转内链
$match_num_from = 1; // 一个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;
}

添加之后如下图

WordPress设置文章自动添加标签内链插图2

然后添加完成了,需要注意一个事项,每次更新主题的时候有可能会把这个文件覆盖,那么需要重新添加一遍,这一点自己勤奋点吧,我这是每回更新主题重新添加。

添加之后的效果图

WordPress设置文章自动添加标签内链插图3

可以看到,添加之后会出现tag内链,点击可以跳转到标签页面WordPress设置文章自动添加标签内链完成了。

© 版权声明
THE END
喜欢就亲吻一下吧
分享
评论 抢沙发
头像
评论一下幻城的文章吧
提交
头像

昵称

图形验证码
取消
昵称代码图片

    暂无评论内容