首页 > 关于wordpress 默认post 地址重写有什办法吗?

关于wordpress 默认post 地址重写有什办法吗?

文章、新闻、案例 这个三个下面都是有内容页的。

文章是 WordPress 默认的post ,新闻和案例都是自定义内容类型。

http://lekee.cc/672.html 这个地址可以访问到文章的内容

http://lekee.cc/news/527.html 这个可以访问到我新闻里的文章

http://lekee.cc/case/239.html 这个也可以访问到案例的文章

但现在我想在文章的 http://lekee.cc/672.html

这个前面加个blog 这个目录。

http://lekee.cc/blog/672.html 这种效果。

但是如果在这里前面加个blog/%post_id%.html 加个blog目录的话,

其他内容文章就用会加blog这个目录了,

http://lekee.cc/news/527.html 会变成 http://lekee.cc/blog/news/527.html

http://lekee.cc/case/239.html 会变成http://lekee.cc/blog/case/239.html

但我只想在 http://lekee.cc/672.html 默认文章里前加这个blog 有没有什么解决办法?


能用代码尽量用代码实现吧,写什么规则都是不靠谱的,如果升级或更换服务器环境都会导致出各种问题,还得重新配置。

// 重定义链接,如果访问的文章类型是post(默认就是post)的,那么则改变url结构。
add_filter('post_type_link', 'reset_post_link', 1, 3);
function reset_post_link( $link, $post = 0 ){
    if ( $post->post_type == 'post' ){
        return home_url( 'blog/'.$post->ID .'.html' );
    } else {
        return $link;
    }
}
// 重新注册文章的链接
add_action( 'init', 'reset_post_init' );
function reset_post_init(){
    add_rewrite_rule(
        'blog/([0-9]+)?.html$',
        'index.php?post_type=post&p=$matches[1]',
        'top' 
    );
    add_rewrite_rule(
        'blog/([0-9]+)?.html/comment-page-([0-9]{1,})$',
        'index.php?post_type=post&p=$matches[1]&cpage=$matches[2]',
        'top' 
    );
}

没问题就点下采纳,有问题就回复告知。


在.htaccess中进行UrlRewrite

【热门文章】
【热门文章】