最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Custom Permalinks for Blog Posts Only

programmeradmin7浏览0评论

I know this is a fairly common topic around here, but I can't seem to figure out a solution to my particular case from reading similar threads.

Basically, my permalink structure needs to be /%postname%/ for compatibility with a finicky plugin. The plugin authors may fix this in the future, but for now I need to come up with my own solution. The problem is, I want my blog post urls to be /blog/%postname%/. I need some way to hook into the URL rewriting for just the post post type and change the permalink structure from /%postname%/ to /blog/%postname%/.

Any ideas?

I know this is a fairly common topic around here, but I can't seem to figure out a solution to my particular case from reading similar threads.

Basically, my permalink structure needs to be /%postname%/ for compatibility with a finicky plugin. The plugin authors may fix this in the future, but for now I need to come up with my own solution. The problem is, I want my blog post urls to be /blog/%postname%/. I need some way to hook into the URL rewriting for just the post post type and change the permalink structure from /%postname%/ to /blog/%postname%/.

Any ideas?

Share Improve this question asked Oct 27, 2011 at 20:40 Dominic PDominic P 7053 gold badges10 silver badges25 bronze badges 9
  • Have you tried changing permalink structure to /blog/%postname%/? – Anh Tran Commented Nov 2, 2011 at 4:11
  • @rilwis, yes, and that structure works perfectly. The problem is that I'm also using Shopp (1.2 Beta) plugin, and for some reason Shopp breaks completely with any permalink structure other than /%postname%/. I know, they should fix this on their end, but I have no idea how long that will take (or if they even will fix it), so I'm trying to find my own work around. – Dominic P Commented Nov 2, 2011 at 23:07
  • Well, I never found a solution to this, but I wound up switching from Shopp to WooCommerce. So, my permalinks are working perfectly now. Thanks to everyone who helped on this. – Dominic P Commented Dec 2, 2011 at 21:51
  • Please add your solution as an answer, so your question does not haunt site as unanswered. – Rarst Commented Jan 27, 2012 at 18:49
  • @Rarst, as I said in my comment, I never found a solution for this unless you consider giving up on the problem a solution. If you like, I can post that I gave up and switched plugins so I wouldn't have to solve this as a solution. – Dominic P Commented Jan 28, 2012 at 2:54
 |  Show 4 more comments

2 Answers 2

Reset to default 15

Even though this is no longer a problem for you, I decided to pursue it for purely academic reasons. I got it working by prepending rewrite rules and filtering permalinks. Note that I wouldn't actually recommend doing this, but it's fun to know it's possible :)

function filter_post_link($permalink, $post) {
    if ($post->post_type != 'post')
        return $permalink;
    return 'blog'.$permalink;
}
add_filter('pre_post_link', 'filter_post_link', 10, 2);


add_action( 'generate_rewrite_rules', 'add_blog_rewrites' );
function add_blog_rewrites( $wp_rewrite ) {
    $wp_rewrite->rules = array(
        'blog/([^/]+)/?$' => 'index.php?name=$matches[1]',
        'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
        'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
        'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
        'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
        'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
        'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
        'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
        'blog/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
        'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
        'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
    ) + $wp_rewrite->rules;
}

If other people are looking for it, here is a variation of Matthew answer (which work perfectly) to achieve this:

Blog archive : /blog/

Blog category : /blog/category-name/

Blog post : /blog/category-name/post-name/

add_action( 'generate_rewrite_rules', 'add_blog_rewrites' );
function add_blog_rewrites( $wp_rewrite ) {
    $wp_rewrite->rules = array(
        'blog/([^/]+)/?$' => 'index.php?taxonomy=category&term=$matches[1]',
        'blog/([^/]+)/page/([0-9]+)/?$' => 'index.php?taxonomy=category&term=$matches[1]&paged=$matches[2]',
        'blog/[^/]+/([^/0-9]+)/?$' => 'index.php?name=$matches[1]',
        'blog/[^/]+/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
        'blog/[^/]+/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
        'blog/[^/]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
        'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
        'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/([^/]+)/[^/]+/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
        'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
        'blog/[^/]+/([^/]+)/[^/]+(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
        'blog/[^/]+/[^/]+/([^/0-9]+)/?$' => 'index.php?attachment=$matches[1]',
        'blog/[^/]+/[^/]+/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
        'blog/[^/]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/[^/]+/([^/])+/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
        'blog/[^/]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]'
    ) + $wp_rewrite->rules;
}

With /%category%/%postname%/ for the permalink structure and this for the categories links:

function filter_category_link($permalink, $term) {
    if($term->taxonomy !== 'category') {
        return $permalink;
    }
    return 'blog' . str_replace('category/', '', $permalink);
}
add_filter('pre_term_link', 'filter_category_link', 10, 2);
发布评论

评论列表(0)

  1. 暂无评论