2013-11-09 30 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

你的代碼完全是邏輯的。但有一個問題。如果我在主頁上並且該網址不包含用戶名,則此頁面上的所有個人資料鏈接都將保持不變,因爲沒有任何內容需要解析。 –