为了给wordpress站点的文章设置tag标签的锚文本,在网上找了一圈方法,最终选择纯代码的方案,亲测有效!
// WordPress 自动为文章添加已使用过的标签
function array2object($array) { // 数组转对象
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else {
$obj = $array;
}
return $obj;
}
function object2array($object) { // 对象转数组
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
$tags = get_tags( array('hide_empty' => false) );
$post_id = get_the_ID();
$post_content = get_post($post_id)->post_content;
if ($tags) {
$i = 0;
$arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序
foreach ( $tags as $tag ) {
// 如果文章内容出现了已使用过的标签,自动添加这些标签
if ( strpos($post_content, $tag->name) !== false){
if ($i == 5) { // 控制输出数量
break;
}
wp_set_post_tags( $post_id, $tag->name, true );
$i++;
}
}
}
}
/* 自动为文章内的标签添加内链 */
$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, '$'),__('【查看更多[%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,可以实现下面的几个功能:
1、遍历文章内容,找到能所有匹配已存在的tags,自动生成文章的tag标签;
2、给文中的tag标签文字形成超链接,点击可以调转到tag标签单页;
3、当文章中出现多个重复文字,只会给第一个添加锚文本超链接;
4、限制了一篇文章只生成不超过5个的超链。

比如上图中,在文章里有tag标签Zooplus和宠物用品,它们都自动生产了锚文本超链接,而下面重复出现的词不会反复添加。
需要注意的是,这个代码区分大小写,比如zooplus如果是小写的z,就不会匹配到。
更加兼容的版本-2025年4月21日更新
// ===============================
// ✅ 自动为文章添加标签(最多添加 5 个)
// ===============================
function array2object($array) {
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
} else {
$obj = $array;
}
return $obj;
}
function object2array($object) {
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
} else {
$array = $object;
}
return $array;
}
add_action('save_post', 'auto_add_tags');
function auto_add_tags($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( wp_is_post_revision($post_id) ) return;
$tags = get_tags(array('hide_empty' => false));
$post = get_post($post_id);
if (!$post || $post->post_type !== 'post') return;
$post_content = $post->post_content;
if ($tags) {
$i = 0;
$arrs = object2array($tags);
shuffle($arrs); // 打乱顺序
$tags = array2object($arrs);
foreach ($tags as $tag) {
if (strpos($post_content, $tag->name) !== false) {
if ($i == 5) break; // 最多添加 5 个标签
wp_set_post_tags($post_id, $tag->name, true);
$i++;
}
}
}
}
// ===============================
// ✅ 自动为文章内容中的标签添加内链(避免正则报错)
// ===============================
$match_num_from = 1; // 标签出现次数下限
$match_num_to = 1; // 每个标签最多添加链接次数
function tag_sort($a, $b) {
return strlen($b->name) - strlen($a->name); // 长标签优先
}
function tag_link($content) {
global $match_num_from, $match_num_to;
if (!is_single()) return $content;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach ($posttags as $tag) {
$keyword = $tag->name;
$link = get_tag_link($tag->term_id);
$url = '<a href="' . esc_url($link) . '" title="查看更多 [' . esc_attr($keyword) . '] 标签的文章" target="_blank">' . esc_html($keyword) . '</a>';
$limit = rand($match_num_from, $match_num_to);
// 使用 preg_replace_callback 避免 lookbehind 报错
$pattern = '/' . preg_quote($keyword, '/') . '/u';
$count = 0;
$content = preg_replace_callback($pattern, function($matches) use (&$count, $limit, $url, $keyword, $content) {
if ($count >= $limit) return $matches[0];
// 检查当前替换是否在已有的链接中(粗略处理)
$pos = strpos($content, '<a');
if ($pos !== false && strpos($content, '</a>') > $pos) {
$before = substr($content, 0, $pos);
if (strpos($before, $matches[0]) !== false) {
return $matches[0]; // 跳过已存在的链接
}
}
$count++;
return $url;
}, $content);
}
}
return $content;
}
add_filter('the_content', 'tag_link');
以上内容添加到主题的function文件内。