WordPress 自动为文章标签(关键词)添加该标签的链接

      WordPress教程分享:有没有自动将文章中出现的标签放上链接?然后多次探索探究找到了自动将文章中出现的标签(关键词)加链接的方法。这方法很简单,将下面的代码添加到主题的 functions.php 即可:

/**
 * WordPress 自动为文章标签添加该标签的链接
 * https://www.wpdaxue.com/auto-add-tag-link.html
 */
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 );

你可以根据自己的需要修改第7行的数值。

原文出自:https://www.wpdaxue.com/auto-add-tag-link.html

© 版权声明
THE END
喜欢就支持一下吧
点赞13赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容