如何在WordPress中删除作者基础

hen*_*ght 7 wordpress rewrite

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)

有谁知道这是否有一个有效的解决方案?

小智 1

尝试:RedirectMatch 301 ^/author/(.+)$ http://www.yourblog.com/ $1

这将进入您的 .htaccess