新闻中心

WordPress的作者url修改与移除以修改教程

作者 / 无忧主机 时间 2014-12-12 21:31:52

有些无忧主机php独立ip空间中的Wordpress的朋友们都会注意到,站长细心一点的话可以发现默认的Wordpress文章作者的存档页面地址为:51php.com/author/name,在每一位用户名前面都会添加一个author的前缀url,如果你觉得这个这个author比较俗,或者不想要这个前缀之类的话,你可以改变这个author或者任何你想要的词。 首先是改变这个词的方法author: 将下面的这段代码添加到你当前Wordpress主题的functions.php文件内:

//更改作者存档前缀 
add_action('init', 'change_author_base');
function change_author_base() {
global $wp_rewrite;
$author_slug = 'profile'; // change slug name
$wp_rewrite->author_base = $author_slug;
}
以上的代码实现了将author 更改为 profile 了,如果你想改成其他的,可以在profile处随意更改。 接着是去掉这个author的方法: 同样在当前主题的functions.php文件内添加如下代码进行实现:
//通过 author_rewrite_rules 钩子添加新的重写规则
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) { 
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users"); 
foreach($authors as $author) {
$author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
$author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
} 
return $author_rewrite;
}

// 通过 author_link 钩子移除前缀 author
add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = preg_replace("|^{$link_base}author/|", '', $link);
return $link_base . $link;
}
注:如果添加代码后,访问新的存档地址出现 404 错误,请访问WP后台 >设置>固定链接,重新保存一次即可。 无忧主机相关文章推荐阅读: WORDPRESS删除自动保存草稿 WORDPRESS忘记密码处理方法总结篇 WORDPRESS主题添加字体大小切换按钮 无忧主机讲解如何禁用WORDPRESS内置的文件编辑器

本文地址:https://www.51php.com/wordpress/17890.html

1
1
1
1
1
1
1

客户服务热线

0791-8623-3537

在线客服