WordPress 文章标签自动链接
之前写过一篇《WordPress 自动内链关键词》,今天和大家分享wordpress文章的标签内链的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * WordPress 文章标签自动链接 */ 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')).'" rel="noopener">'.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 ); |