我們都知道,WordPress文章作者的存檔頁面地址都是類似 http://domain.com/author/cmhello 這樣的,在用戶名前面會添加“author”前綴。今天倡萌就分享下更改或者移除這個前綴的方法。

更改作者存檔前綴 author
比如將 http://domain.com/author/cmhello 修改為 http://domain.com/profile/cmhello 樣式,并且支持作者存檔頁面的Feed輸出。
將下面的代碼添加到當(dāng)前主題的 functions.php 即可:
//更改作者存檔前綴
add_action('init', 'wpdaxue_change_author_base');
function wpdaxue_change_author_base() {
global $wp_rewrite;
$author_slug = 'profile'; // 更改前綴為 profile
$wp_rewrite->author_base = $author_slug;
}
上面的代碼就將前綴 author 更改為 profile 了,請根據(jù)自己的實際,修改第 5行的前綴。
參考資料:http://wp-snippet.com/snippets/change-the-author-slug-url-base/
注:如果添加代碼后,訪問新的存檔地址出現(xiàn) 404 錯誤,請訪問WP后臺 >設(shè)置>固定鏈接,重新保存一次即可。下同。
移除作者存檔前綴 author
將原來的 http://domain.com/author/cmhello 修改為 http://domain.com/cmhello ,并且支持作者存檔頁面的Feed輸出。
//通過 author_rewrite_rules 鉤子添加新的重寫規(guī)則
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;
}
參考資料:http://wp-snippet.com/snippets/remove-author-prefix-from-slug/,原文方法不支持作者存檔的feed輸出,倡萌添加第 8 行代碼,使之支持。





我的wordpress 默認(rèn)的xxx.com/author/xxx頁面顯示404,請問是什么問題?
沒反應(yīng) 啊