WordPress 中的标准作者链接如下所示:example.com/author/johnsmith
我想删除author/
URL 的一部分,因此用户名位于根目录中。例如:example.com/johnsmith
我控制网站上的页面创建,因此页面和作者姓名不会发生冲突。
到目前为止,我已经尝试过以下解决方案WP 片段但这似乎不再有效:
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;
}
有谁知道这个问题是否有可行的解决方案?
我已经测试了这个组合解决方案,但在重新生成永久链接之前不起作用。您可以做到这一点,正如 brasfolio 所描述的那样:只需单击仪表板中的永久链接页面上的“保存”即可。
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)