代码是由一位大佬多次探索探究后找到了自动将文章中出现的标签加链接的方法。这方法很简单,将下面的代码添加到主题的 functions.php 就可以了:
/**
* WordPress 自动为文章标签添加该标签的链接
* https://www.aaym.net/2719.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行的数值。
本文结束END

© 版权声明
THE END
暂无评论内容