2013-11-09 28 views
1

我正在尝试更改用户配置文件的链接,而且看起来我太新手了。 链接的当前结构是:domain.com/profile/username。 我想要的是像:domain.com/username/city其中城市从wp_postmeta表Wordpress用户固定链接更改问题

我用这个函数试图采取的东西:

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

你不想叫'get_current_user_id()',而应该在URL解析'username'并获取其'city'值改写 – jasonslyvia

+0

它有可能做到这一点?你能帮我吗? –

+0

有可能使用foreach循环来获取所有ID吗? –

回答

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; 

} 
+0

你的代码完全是逻辑的。但有一个问题。如果我在主页上并且该网址不包含用户名,则此页面上的所有个人资料链接都将保持不变,因为没有任何内容需要解析。 –