纯代码让 WordPress 文章自动添加本站标签内链

做网站写文章无非是希望搜索引擎多收录、访客多浏览,标签是一个吸引蜘蛛和提升访客停留时间的好方法,但是手动添加标签费事费力,最好的方法是使用插件或代码来自动完成工作。

我们的需求是:只要是站内出现过的标签,文章都自动添加上对应的内链,有效地提升不同文章、分类间的关联度。

编写网站

眼哥尝试了许多插件,例如 Simple Tags,但是并没有起到作用,也许和其他插件产生了冲突。而且我们只需要这么一个简单的功能,用插件有点小题大做。

通过搜索找到了一段代码,但是仅仅用这段代码的话只会在文章中添加当前文章的标签链接,没有达到我们的要求:

/* 自动为文章内的标签添加内链 */
$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;
}
add_filter('the_content','tag_link',1);

继续搜索发现一个新方法,发布、保存或更新文章时检测文章中的关键字,如果匹配到之前用过的标签则自动给当前文章添加这个标签,再由上面的这个代码给标签添加内链。

整合的代码如下,将其拷贝到主题的 functions.php 的结尾就行:

/**
 * 自动给 WordPress 文章添加站内标签和内链
 * https://www.idcbuy.net/wordpress/wordpress-auto-add-tag-links/
 * /

/* 自动为文章添加标签 */
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) {
        foreach ( $tags as $tag ) {
            // 如果文章内容出现了已使用过的标签,自动添加这些标签
            if ( strpos($post_content, $tag->name) !== false)
                wp_set_post_tags( $post_id, $tag->name, true );
        }
    }
}

/* 自动为文章内的标签添加内链 */
$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;
}
add_filter('the_content','tag_link',1);

这个代码是目前网上能找到的最直接有效的方法,但是它也还有不足:

  1. 添加代码后需要重新保存或更新以前发布的文章,代码才能自动添加站内已有的标签和内链;
  2. 随着站内的标签越来越多,内容与标签重合的几率就会变大,新发布的文章很可能会生成大量的标签,而其中部分标签与文章中心并无太大关联。

希望有人能够改进代码,比如在检测和添加标签的部分像设置内链的部分那样添加一个次数的筛选,当文章内同一个关键词多次出现时才将它提取出来作为标签。

耕读君
耕读君

云时代的我们要学会“腾云驾雾”

文章: 184

留下评论

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