仅发布链接当前:http://mydomain/post-name 我只想更改帖子链接:http://mydomain/blog/post-name
答案 0 :(得分:2)
在永久链接页面(设置>永久链接)中,选择最后一个选项以输入自定义永久链接结构并输入:
/blog/%postname%/
它会在您的所有帖子前加/blog/
。
<强>更新强>
要仅为帖子添加前缀,请在functions.php
:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'blog/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function change_blog_links($post_link, $id=0)
{
$post = get_post($id);
if( is_object($post) && $post->post_type == 'post'){
return home_url('/blog/'. $post->post_name.'/');
}
return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);