Wordpress用户固定链接更改问题

时间:2013-11-09 23:00:40

标签: wordpress permalinks

我试图更改用户个人资料的链接,而且似乎我太棒了。 该链接的当前结构是: domain.com/profile/username 。 我想要的是: domain.com/username/city 其中 city 取自wp_postmeta表

我尝试使用此功能:

add_action('init', 'wpse82004_init');

function wpse82004_init()

{

    global $wp_rewrite;

    $city = get_user_meta( get_current_user_id(), 'city', TRUE ); 

    $wp_rewrite->author_base = $city;

    $wp_rewrite->author_structure = '/%author%' . '/' . $wp_rewrite->author_base;

}

问题是返回我点击的所有配置文件中当前登录用户的城市。 任何帮助,将不胜感激。感谢

1 个答案:

答案 0 :(得分:2)

未经测试的代码

add_action('init', 'wpse82004_init');

function wpse82004_init()

{

    global $wp_rewrite;

    //parse username from url
    $url = $_SERVER["REQUEST_URI"];
    $username = preg_replace('/^.*profile\/(.*)$/i', '$1', $url);

    //get user by username
    $user = get_user_by('slug', $username);

    //rewrite the city value of anticipated user other than current user
    $city = get_user_meta( $user->ID, 'city', TRUE ); 

    $wp_rewrite->author_base = $city;

    $wp_rewrite->author_structure = '/%author%' . '/' . $wp_rewrite->author_base;

}