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)
這個方法,所以可以直接調用來替換category
的base
(默認爲category
)。