1. 程式人生 > >Wordpress修改Author頁面URL地址

Wordpress修改Author頁面URL地址

Wordpress預設Author頁面http://mysite.com/author/authorname,若想要將author替換成自定義的slug,那麼我們需要使用$wp_rewrite這個全域性物件。

functions.php中,新增以下程式碼:

function edit_author_base()
{
    global $wp_rewrite;
    $author_slug = 'custom-author-slug';
    $wp_rewrite->author_base = $author_slug;
}
add_action('init', 'edit_author_base'
);

這樣一來,當使用get_author_posts_url等函式時,獲得的URL中將會是http://mysite.com/custom-author-slug/authorname的形式。若要替換成其他值,請自行將$author_slug這個變數的值改為對應字串即可。

注:WP_Rewrite這個類中有set_category_base($category_base)這個方法,所以可以直接呼叫來替換categorybase(預設為category)。