如何删除WordPress中的作者库

如何删除WordPress中的作者库,wordpress,rewrite,Wordpress,Rewrite,WordPress中的标准作者链接如下:example.com/author/johnsmith 我想删除URL的作者/部分,以便用户名位于根目录中。例如:example.com/johnsmith 我在我的网站上控制页面创建,所以页面和作者姓名不会有冲突 到目前为止,我已经尝试了以下解决方案,但这似乎不再有效: add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules'); function no_author_base_

WordPress中的标准作者链接如下:
example.com/author/johnsmith

我想删除URL的
作者/
部分,以便用户名位于根目录中。例如:
example.com/johnsmith

我在我的网站上控制页面创建,所以页面和作者姓名不会有冲突

到目前为止,我已经尝试了以下解决方案,但这似乎不再有效:

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;
}

有人知道这是否有可行的解决方案吗?

试试:RedirectMatch 301^/author/(.+)$$1


这将进入你的.htaccess

这个问题类似于

你可以这样做。 这几乎就是你想要的


希望这能起作用…

我已经测试过这种组合解决方案,但在永久链接重新生成之前不起作用。正如brasfolio所描述的,您可以这样做:只需在仪表板的permalink页面上单击save

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;
}

您的代码看起来很好。手动刷新permalink结构以反映这些更改

1)  Settings -> Permalinks -> choose default -> Save
2)  Revert the settings to original.

为了重定向提要,还可以在其他两个类似的字段附近添加此字段:

$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';

似乎我们必须刷新permalinks结构,但每次有人注册新规则时

因此,请尝试使用以下命令自动刷新它:

$wp_rewrite->flush_rules();
在functions.php文件中

我也有同样的问题,但这似乎是好的。
但是仍然存在与页面和帖子名称冲突的问题

有几个WP插件提供这种功能。在我搜索了WP存储库,但找不到任何WP存储库时,您最好询问一下。你能提供一些链接吗?“author slug”在WP插件库上搜索时发现了几个。对不起,我应该说我找不到“工作”插件:)我在[WP hackers]、core.trac.wordpress.org和中深度搜索了一下。也适用于一些
htaccess
,请参见。找不到任何工作解决方案:/。现在,我只能考虑为每个作者创建一个页面/帖子(在这里我们可以轻松设置永久链接),或者用钩子做一些事情。嗨,谢谢你的回答。您链接到的解决方案是否与我说的不再有效的解决方案相同?请查看我的问题。我已删除我的答案(使用
global$wp\u rewrite
),直到能够完全调试它。您需要一个用于wp\u rewrite的筛选器。其他方式,当处理url时,它将有404错误(将其视为帖子)。使用author_rewrite_规则事实上我今天已经设法让它工作了(您的代码带有add_filter code from question)。试试看。嗨,迈克,谢谢你。它似乎对我还是不起作用?不知道为什么!我认为您的方法(例如,
作者重写规则
和b_uuuu的方法)的组合可能会起作用@b_u;您是否能够重新发布您的解决方案,以便我们都可以尝试调试它?再次感谢大家你的问题出在哪里:页面上的作者链接是错误的,或者当使用它们是404时?使用这种方法,什么都不会发生。我的作者链接仍然是
site.com/author/user
@b_uu的方法产生了像
site.com/user
这样的作者链接,但破坏了我网站上的其余页面。