一般wp默认的标签页的链接名字都是直接中文的,所以就会有乱码,这在seo优化中是非常不好的,对搜索引擎不够友好,所以我们需要将标签页修改为id的形式显示从而达到优化seo的目的
下面我们只需要一行代码即可
将下面的代码放入主题文件的function.php文件中
//标签以id形式展现 xbnb.cn
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;
}
晓白博客网版权所有,原文地址https://www.xbnb.cn/365
© 版权声明
THE END
请登录后查看评论内容