WordPress给文章标签自动添加内链
很多做SEO的朋友都应该知道文章内链的重要性。内部链接结构可以引导权值的传递,提高收录,提升内页排名。 如何给WordPress博客的文章关键词标签自动添加内链呢?其实我们只需要在主题目录下的functions.php文件中添加一段代码就可以实现了。
// by 百谷博客文章添加关键词链接开始function wpkj_auto_add_tag_link($content){$limit = 1; // 设置同一个标签添加几次链接$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', 'wpkj_auto_add_tag_link', 1 );// by 百谷博客文章添加关键词链接结束// by 百谷博客文章添加关键词链接开始 function wpkj_auto_add_tag_link($content){ $limit = 1; // 设置同一个标签添加几次链接 $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', 'wpkj_auto_add_tag_link', 1 ); // by 百谷博客文章添加关键词链接结束// by 百谷博客文章添加关键词链接开始 function wpkj_auto_add_tag_link($content){ $limit = 1; // 设置同一个标签添加几次链接 $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', 'wpkj_auto_add_tag_link', 1 ); // by 百谷博客文章添加关键词链接结束
感谢您的来访,获取更多精彩文章请收藏本站。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容