阅读以下建议后Jan Fabry 关于URL重写和作者slug,我接受了将这两种解决方案结合在一起的挑战。
Goal: 其目的是为作者建立如下结构:
http://domain.tld/employees/(department|group)/firstname-lastname
Current solution: 为了安排这一点,我将参考答案中提到的解决方案合并如下:
// NOTE: This need to be stored different and need to be attached to author/profile
// Will be added to user settings so this can be changed (only primary group will be stored)
$wpa04142013_author_groups = array( \'staff\', \'admin\', \'support\', \'itnerds\', \'etc\' );
/*
* Init WordPress to add globals which can be used for the authors and levels of the authors.
*/
add_action( \'init\', \'wpa04142013_init\' );
function wpa04142013_init()
{
global $wp_rewrite;
$author_groups = $GLOBALS[\'wpa04142013_author_groups\'];
// Define the tag and use it in the rewrite rule
add_rewrite_tag( \'%author_group%\', \'(\' . implode( \'|\', $author_groups ) . \')\' );
$wp_rewrite->author_base = \'employees/%author_group%\';
}
上述解决方案将确保
author_base
根据开始定义的目标根据需要进行更改。之后
author_link
需要连接到“其他”才能使用指向作者存档页的正确链接。
add_filter( \'author_link\', \'wpa04142013_author_link\', 10, 3 );
function wpa04142013_author_link( $link, $author_id, $author_nicename )
{
//NOTE: This if/else needs to be changed --> either select case and use
// global defined groups based upon user property
if ( 1 == $author_id ) {
$author_group = \'staff\';
} else {
$author_group = \'admin\';
}
$link = str_replace( \'%author_group%\', $author_group, $link );
//Below solution added from other WordPress Answers suggestion
$author_nickname = get_user_meta( $author_id, \'nickname\', true );
if ( $author_nickname ) {
$link = str_replace( $author_nicename, $author_nickname, $link );
}
return $link;
}
实现上述链接后,指向作者的链接正常工作,但请求和url重写不起作用。单击作者的链接将生成模板的404页。
如果我没有弄错的话,我还需要修改下面的部分,但这就是问题所在。
/*
* Hook into \'request\' to modify the author request.
* Change the way the lookup works (via nickname in stead of the slug)
*/
add_filter( \'request\', \'wpa04142013_request\' );
function wpa04142013_request( $query_vars )
{
if ( array_key_exists( \'author_name\', $query_vars ) ) {
global $wpdb;
$author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key=\'nickname\' AND meta_value = %s", $query_vars[\'author_name\'] ) );
if ( $author_id ) {
$query_vars[\'author\'] = $author_id;
unset( $query_vars[\'author_name\'] );
}
}
return $query_vars;
}
add_filter( \'author_rewrite_rules\', \'wpa04142013_author_rewrite_rules\' );
function wpa04142013_author_rewrite_rules( $author_rewrite_rules )
{
foreach ( $author_rewrite_rules as $pattern => $substitution ) {
if ( FALSE === strpos( $substitution, \'author_name\' ) ) {
unset( $author_rewrite_rules[$pattern] );
}
}
return $author_rewrite_rules;
}
我已经得到了一些帮助
Dutch WordPress support forum, 但我仍在寻找正确的解决方案。还使用重写分析器查看了重写,但找不到
author_group
. 希望你能帮我指出正确的方向。