這可以通過正是你所改變的時候同樣的方法添加新重寫規則爲每個用戶或刪除筆者基地完成。所以,從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;
}
我有幾個問題關於這個if可能的。 1.`author_rewrite_rules`是否在每頁加載時運行? 2.你是否只能刪除循環,因爲用戶被他們的ID引用?如果用戶被他們的用戶名稱引用,該怎麼辦? – henrywright 2014-03-15 16:55:14