不需要插件将wordpress文章tag标签链接改成id.html

wordpress文章tag标签默认链接是用中文字显示的,这样不利于seo,下面web建站小编教大家如何把链接改成id.html(非插件)。

打开主题functions.php,在最下面插入以下代码:

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).html' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  	'tag/(\d+)/(feed|rdf|rss|rss2|atom).html' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  	'tag/(\d+)/embed.html' => 'index.php?tag_id=$matches[1]&embed=true',
  	'tag/(\d+)/page/(\d+).html' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
  	'tag/(\d+).html' => '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.'.html');
  }
  return $link;
}
function tag_query_vars($public_query_vars) {
  $public_query_vars[] = 'tag_id';
  return $public_query_vars;
}

现在相关链接已经改变了,但是页面是404,所以这时需要重新打开“固定链接”页面重新保存即可!

本文来自投稿,不代表科技代码立场,如若转载,请注明出处https://www.cwhello.com/119997.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
上一篇 2022年11月7日 08:43
下一篇 2022年11月8日 01:01

相关推荐

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息