您好!欢迎来到模板下载吧!本站资源24小时自动发货,请放心选购,一次付费,终身下载,售后请提交工单!

更改/移除WordPress作者存档页面的前缀“author”

半岛未凉 2018-01-22 快速入门 2168 已收录 本文共1861个字,预计阅读需要5分钟。
  • 文章介绍
  • 快速入门
  • 增值服务

我们都知道,WordPress文章作者的存档页面地址都是类似 http://domain.com/author/cmhello 这样的,在用户名前面会添加“author”前缀。今天就分享下更改或者移除这个前缀的方法。

更改作者存档前缀 author
比如将 http://domain.com/author/cmhello 修改为 http://domain.com/profile/cmhello 样式,并且支持作者存档页面的Feed输出。

将下面的代码添加到当前主题的 functions.php 即可:

  1. //更改作者存档前缀 
  2. add_action('init', 'mbxzb_change_author_base');
  3. function mbxzb_change_author_base() {
  4.     global $wp_rewrite;
  5.     $author_slug = 'profile'; // 更改前缀为 profile
  6.     $wp_rewrite->author_base = $author_slug;
  7. }

上面的代码就将前缀 author 更改为 profile 了,请根据自己的实际,修改第 5行的前缀。

参考资料:http://wp-snippet.com/snippets/change-the-author-slug-url-base/

注:如果添加代码后,访问新的存档地址出现 404 错误,请访问WP后台 >设置>固定链接,重新保存一次即可。下同。

移除作者存档前缀 author
将原来的 http://domain.com/author/cmhello  修改为 http://domain.com/cmhello ,并且支持作者存档页面的Feed输出。

  1. //通过 author_rewrite_rules 钩子添加新的重写规则  
  2. add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');  
  3. function no_author_base_rewrite_rules($author_rewrite) {   
  4.     global $wpdb;  
  5.     $author_rewrite = array();  
  6.     $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");      
  7.     foreach($authors as $author) {  
  8.         $author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';  
  9.         $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';  
  10.         $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';  
  11.     }     
  12.     return $author_rewrite;  
  13. }// 通过 author_link 钩子移除前缀 author  
  14. add_filter('author_link', 'no_author_base', 10002);  
  15. function no_author_base($link, $author_id) {  
  16.     $link_base = trailingslashit(get_option('home'));  
  17.     $link = preg_replace("|^{$link_base}author/|"'', $link);  
  18.     return $link_base . $link;  
  19. }  

参考资料:http://wp-snippet.com/snippets/remove-author-prefix-from-slug/,原文方法不支持作者存档的feed输出,添加第 8 行代码,使之支持。

温馨提示:本文最后更新于2019年3月19日,已超过 2 年没有更新,如果文章内容或图片资源失效,请留言反馈,模板下载吧会及时处理,谢谢!

上一篇:

下一篇:

更改/移除WordPress作者存档页面的前缀“author”:等您坐沙发呢!
大牛,别默默的看了,快来点评一下吧!:)。

您必须登录后才能发表评论哦!:)

站内登录 QQ登录 微博登录
wordpress自适应高级图片shejigh主题

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系作者

模板下载吧,累计帮助1000+用户成功建站,为草根创业提供助力!

立刻开启你的建站之旅
现在加入模板下载吧,注册一个账号
';
  • 模板下载吧拥有海量网站模板及源码,站长亲测干净无后门。

  • 注册即能下载免费模板栏目资源,帮您更快的完成网站建设。

  • 每日更新模板资源,每日精品推荐,及时获取最新模板资源流行去向。

  • 完美的售后服务,帮助草根站长、企业等成功建站。

  • 将您最爱的资源收藏,建立自己的资源库,并与朋友分享。