我设置了一个WordPress网站,我希望在查看单个帖子时在网址中显示博客目录:
mysite.com/blog/my-first-post
代替mysite.com/my-first-post
任何想法如何实现这一目标?
答案 0 :(得分:4)
/blog/%postname%/
来自WordPress Codex:http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder
答案 1 :(得分:1)
我知道这个问题很旧,但是有人可能会从我的回答中受益:
有两个部分:添加规则,使/ blog / article-slug和/ blog / paged /指向正确的URL,还将所有子段修改为新链接:
function THEME_SLUG_posts_add_rewrite_rules( $wp_rewrite )
{
$new_rules = [
'articles/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged='. $wp_rewrite->preg_index(1),
'articles/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
];
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'THEME_SLUG_posts_add_rewrite_rules');
function THEME_SLUG_posts_change_blog_links($post_link, $id=0){
$post = get_post($id);
if( is_object($post) && $post->post_type == 'post'){
return home_url('/articles/'. $post->post_name.'/');
}
return $post_link;
}
add_filter('post_link', 'THEME_SLUG_posts_change_blog_links', 1, 3);
这里是在博客文章标题之前添加/ articles /的示例。不要忘记刷新重写规则,例如通过保存永久链接设置