WordPress怎么设置内链?文章内容标签自动加锚文本优化

由于WordPress文章自动锚文本插件Automatic Internal Links for SEO不支持标签自动锚文本的功能。

如果能够添加标签关键词锚文本,这样不仅可以SEO优化我们的内部链接,还能让用户参考相关文章。

WordPress标签如何自动添加内链?

那么如何让WordPress网站上的文章,自动添加标签内链呢?

其实我们只需要在主题目录下的functions.php文件中,添加一段php代码就可以实现。

WordPress文章内容标签自动添加锚文本内链

/**
* WordPress文章内容标签自动加锚文本内链
* https://www.chenweiliang.com/cwl-27651.html
**/
function wptag_auto_add_anchor_text_link($content){

$limit = 1; // 设置WordPress文章同一个标签,自动添加几次内链?

$posttags = get_the_tags();

if ($posttags) {
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;

$cleankeyword = stripslashes($keyword);
$url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')).'">'.addcslashes($cleankeyword, '$').'</a>';
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
$content = preg_replace($regEx,$url,$content,$limit);
}
}

return $content;
}
add_filter( 'the_content', 'wptag_auto_add_anchor_text_link', 1 );

添加代码到WordPress主题的functions.php文件后,再看我们的文章。

当我们添加的标签关键词出现时,是否有自动添加内部链接?

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注