大家都知道wordpress标签链接默认是以tag slug结尾,如果没写slug,会自动填充为标签名,标签名是中文的话,链接会非常长,不美观也不利于seo,那怎么把wordpress tag链接中标签slug改为id呢?
wordpress tag链接标签slug改为id
只需要将下面php代码加入到主题的functions.php文件中就可以了。
// wordpress tag链接标签slug改为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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 xxx@163.com 举报,一经查实,本站将立刻删除。