仅向帖子的新闻类别和子类别的固定链接添加URL前缀

时间:2018-01-19 作者:Samuel Afolabi

我改变了http://domain.com/%postname%/http://domain.com/news/%postname%/ 使用此处提供的答案:Add a URL prefix to permalinks of one category of posts only. 但我对新闻本身的分类有问题。如何使此功能适用于子类别

1 个回复
SO网友:Dharmishtha Patel

add_action( \'init\', \'wpa_category_base\' );
function wpa_category_base() {
    // Remember to flush the rules once manually after you added this code!
    add_rewrite_rule(
        // The regex to match the incoming URL
        \'news/([^/]+)/([^/]+)/([^/]+)(/[0-9]+)?/?$\',
        // The resulting internal URL
        \'index.php?category_name=$matches[1]/$matches[2]&name=$matches[3]&paged=$matches[4]\',

        // Add the rule to the top of the rewrite list
        \'top\' );
}
自定义永久链接结构:/media/%category%/%postname%/类别库:。

为我工作,WordPress 3.4.1,没有任何相关插件。

尝试

add_action( \'init\', \'wpa_category_base\' );
function wp_category_base() {
// Remember to flush the rules once manually after you added this code!
    add_rewrite_rule(
    // The regex to match the incoming URL
    \'news/([^/]+)/([^/]+)/([^/][^f][^e][^e][^d]+)(/[0-9]+)?/?$\', //here my changes to exclude \'feed\'
    // The resulting internal URL
    \'index.php?category_name=$matches[1]/$matches[2]&name=$matches[3]&paged=$matches[4]\',
    // Add the rule to the top of the rewrite list
    \'top\' );
}

结束

相关推荐

Post with Custom Permalinks

我的永久链接设置如下:http://myblog.com/%category%/%postname%/一切正常。但我正在寻找一种方法,只为一些帖子(10-11篇帖子)设置如下永久链接。http://myblog.com/%postname%/我之所以要这样做,是因为我正在合并两个WordPress网站,我不想失去另一个网站的帖子,这些帖子已经以旧的permalink结构发布在Facebook等网站上。