如何在WordPress中删除作者库 [英] How to remove author base in WordPress
问题描述
WordPress 中的标准作者链接如下所示:example.com/author/johnsmith
Standard author links in WordPress look like: example.com/author/johnsmith
我想删除 URL 的 author/
部分,以便用户名位于根目录中.例如:example.com/johnsmith
I'd like to remove the author/
part of the URL so the username is in the root. For example: example.com/johnsmith
我控制我网站上的页面创建,因此页面和作者姓名不会发生冲突.
I control page creation on my site so there will be no chance of a conflict in page and author name.
到目前为止,我已经尝试了 WP Snippet 的以下解决方案,但这似乎不再上班:
So far I've tried the following solution from WP Snippet but this no longer seems to work:
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;
}
有人知道有没有可行的解决方案吗?
Do anyone know if there is a working solution to this?
推荐答案
我已经测试了这个组合解决方案,但在重新生成永久链接之前无法正常工作.您可以做到,正如 brasfolio 所描述的:只需单击仪表板中永久链接页面上的保存即可.
I've tested this combined solution but wasn't working before regenerating of permalinks. You can do it, as brasfolio described : simply clicking save on permalink page in dashboard.
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;
}
if( !is_admin() ) {
add_action('init', 'author_rewrite_so_22115103');
}
function author_rewrite_so_22115103() {
global $wp_rewrite;
if( 'author' == $wp_rewrite->author_base ) $wp_rewrite->author_base = null;
}
这篇关于如何在WordPress中删除作者库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!