仅适用于博客文章的自定义固定链接

时间:2011-10-27 作者:Dominic P

我知道这在这里是一个相当常见的话题,但我似乎无法通过阅读类似的帖子找到解决我的特殊情况的方法。

基本上,我的permalink结构需要是/%postname%/才能与挑剔的插件兼容。插件作者将来可能会解决这个问题,但现在我需要拿出自己的解决方案。问题是,我希望我的博客帖子URL为/blog/%postname%/。我需要一些方法来连接到URL重写,以便post 文章类型,并将永久链接结构从/%postname%/更改为/博客/%postname%/。

有什么想法吗?

2 个回复
最合适的回答,由SO网友:Matthew Boynes 整理而成

尽管这对你来说不再是一个问题,但我决定追求它纯粹是出于学术原因。我通过预先设置重写规则和过滤永久链接来实现它。请注意,我实际上不建议这样做,但知道这是可能的很有趣:)

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;
}

SO网友:vard

如果其他人正在寻找它,这里有一个马太答案的变体(它工作得很好)来实现这一点:

博客存档:/blog/

博客类别:/blog/category-name/

博客帖子:/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;
}
使用/%category%/%postname%/ 对于permalink结构和类别链接:

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);

结束

相关推荐