【发布时间】:2013-05-04 21:27:06
【问题描述】:
我想实现自定义作者网址。
目前作者网址是这样的: http://site.com/author/author-name/
我想做类似的事情 http://site.com/my-custom-url-here
对于每个单独的用户。
我尝试使用author_rewrite_rules 过滤器,使用以下代码,这可以正确转换 url,但是当我浏览 url 时,这会给我一个找不到页面的消息
add_filter('author_link', 'no_author_base', 1000, 3);
function no_author_base($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = preg_replace("|^{$link_base}author/|", '', $link);
return $link_base . $link;
}
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})/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;
}
任何帮助将不胜感激!
谢谢。 更新
问题解决了!,基本上我不知道调用flush_rewrite_rules函数。
【问题讨论】:
-
实现代码后,是否访问了 admin -> permalinks 页面刷新了重写规则?
-
@diggy 我不知道,我在这里了解到:wordpress.stackexchange.com/questions/5413/… 看我的回答 :)
标签: wordpress