2011-02-18 126 views
2

我知道如何改變作者永久鏈接的基礎,但是在我的網站上,我指的不是用戶名而是基於用戶ID的數字,所以用戶編號5寫了這篇文章,而是比JohnDoe123寫了這篇文章。WordPress的作者固定鏈接

問題來了,當我去用戶檔案,而不是看到像example.com/authors/5/我看到example.com/authors/johndoe123/。

如何更改固定鏈接,以便使用以下結構提取作者檔案? :

[wordpress_site_url] /作者/ [USER_ID]/

回答

5

這可以通過正是你所改變的時候同樣的方法添加新重寫規則爲每個用戶或刪除筆者基地完成。所以,從previous answer適應代碼,你會增加你的重寫規則是這樣的:

add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules'); 
function my_author_url_with_id_rewrite_rules($author_rewrite) { 
    global $wpdb; 
    $author_rewrite = array(); 
    $authors = $wpdb->get_results("SELECT ID, user_nicename AS nicename from {$wpdb->users}");  
    foreach ($authors as $author) { 
    $author_rewrite["authors/{$author->ID}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]'; 
    $author_rewrite["authors/{$author->ID}/?$"] = "index.php?author_name={$author->nicename}"; 
    } 
    return $author_rewrite; 
} 

,然後篩選作者鏈接:

add_filter('author_link', 'my_author_url_with_id', 1000, 2); 
function my_author_url_with_id($link, $author_id) { 
    $link_base = trailingslashit(get_option('home')); 
    $link = "authors/$author_id"; 
    return $link_base . $link; 
} 

其實我不認爲你需要在這種情況下,爲每個用戶創建規則,以下兩條規則就足夠了:

add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules'); 
function my_author_url_with_id_rewrite_rules($author_rewrite) { 
    $author_rewrite = array(); 
    $author_rewrite["authors/([0-9]+)/page/?([0-9]+)/?$"] = 'index.php?author=$matches[1]&paged=$matches[2]'; 
    $author_rewrite["authors/([0-9]+)/?$"] = 'index.php?author=$matches[1]'; 
    return $author_rewrite; 
} 
+0

我有幾個問題關於這個if可能的。 1.`author_rewrite_rules`是否在每頁加載時運行? 2.你是否只能刪除循環,因爲用戶被他們的ID引用?如果用戶被他們的用戶名稱引用,該怎麼辦? – henrywright 2014-03-15 16:55:14