0

我正在尝试更改用户个人资料的链接,看来我太菜鸟了。链接的当前结构是:domain.com/profile/username。我想要的是:domain.com/username/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;

}

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

4

1 回答 1

1

未经测试的代码

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;

}
于 2013-11-10T01:51:47.637 回答