WordPress中的标准作者链接如下所示: example.com/author/johnsmith
我想删除author/URL 的一部分,以便用户名在根目录中.例如:example.com/johnsmith
我在我的网站上控制页面创建,因此页面和作者姓名不会发生冲突.
到目前为止,我已经尝试了WP Snippet的以下解决方案,但这似乎不再起作用:
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;
}
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;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这是否有一个有效的解决方案?
| 归档时间: |
|
| 查看次数: |
2660 次 |
| 最近记录: |