2011-02-18 130 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