前言:
我们都知道wordpress文章标签默认是中文,比如标签少的时候可以自己修改别名,比如拼音或者英文,但是有一个缺点就是同音词的时候不好区分,只能在别名后面加-1,-2 这样的区分,比如手机,收集,首级,遇到同音词的时候就很不好,展示方式看个人喜欢,除了中文展示会转码外,并没什么卵用,个人觉得用 ID 方式最好,比如 Discuz 的标签展示方式就最好。
其实最重要的是中文TAG标签会在某些浏览器被转义成乱码,个人觉得不好看!
将以下代码加在主题目录的 functions.php 中即可实现,如果想要ID以html结尾就更改return home_url('/tag/'.$term->term_id);成
return home_url('/tag/'.$term->term_id.'.html');即可。
注意:添加代码后需要进入后台重新保存一下固定链接。
/* 标签以id方式展示*/ add_action('generate_rewrite_rules','tag_rewrite_rules'); add_filter('term_link','tag_term_link',10,3); add_action('query_vars', 'tag_query_vars'); function tag_rewrite_rules($wp_rewrite){ $new_rules = array( 'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]', 'tag/(\d+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?tag_id=$matches[1]&feed=$matches[2]', 'tag/(\d+)/embed/?$' => 'index.php?tag_id=$matches[1]&embed=true', 'tag/(\d+)/page/(\d+)/?$' => 'index.php?tag_id=$matches[1]&paged=$matches[2]', 'tag/(\d+)/?$' => 'index.php?tag_id=$matches[1]', ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } function tag_term_link($link,$term,$taxonomy){ if($taxonomy=='post_tag'){ return home_url('/tag/'.$term->term_id); } return $link; } function tag_query_vars($public_query_vars){ $public_query_vars[] = 'tag_id'; return $public_query_vars; }
添加代码后链接样式
显示于标签相同的所有标签列代码:同意是代码加在主题目录的 functions.php 中即可实现
/* 自动为文章内的标签添加内链 */ $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);
另一个:
//WordPress文章关键词自动内链 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){ $match_num_from = 1; //一个标签少于几次不链接 $match_num_to = 1; //一个标签最多链接几次 $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 = "".addcslashes($cleankeyword, '$').""; $limit = rand($match_num_from,$match_num_to); //不链接代码 $content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</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 . ')(?!(([^<>]*?)>)|([^>]*?))\'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);
发表评论