當(dāng)前位置:首頁>WordPress建站>基礎(chǔ)教程>更改/移除WordPress作者存檔頁面的前綴“author”

更改/移除WordPress作者存檔頁面的前綴“author”

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

change-or-remove-the-author-prefix-wpdaxue_com

更改作者存檔前綴 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 行代碼,使之支持。

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
基礎(chǔ)教程

給WordPress自定義文章類型添加短鏈接

2013-6-21 8:57:32

基礎(chǔ)教程

修改WordPress搜索結(jié)果的鏈接樣式

2013-6-24 11:57:31

2 條回復(fù) A文章作者 M管理員
  1. ALU

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

  2. 沒反應(yīng) 啊

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

中超| 平阳县| 古田县| 卢氏县| 海盐县| 都安| 阜宁县| 瓦房店市| 莎车县| 兰西县| 神池县| 苍山县| 铅山县| 平利县| 马龙县| 色达县| 关岭| 陆丰市| 高青县| 屏边| 金山区| 漠河县| 濮阳县| 繁峙县| 炎陵县| 萨嘎县| 准格尔旗| 新宁县| 乌兰察布市| 永吉县| 桐梓县| 横山县| 祥云县| 常州市| 广丰县| 安庆市| 平塘县| 铅山县| 旌德县| 临湘市| 三台县|