Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
特定类别的WordPress自定义重写规则_Wordpress_.htaccess_Url Rewriting_Permalinks - Fatal编程技术网

特定类别的WordPress自定义重写规则

特定类别的WordPress自定义重写规则,wordpress,.htaccess,url-rewriting,permalinks,Wordpress,.htaccess,Url Rewriting,Permalinks,我有一个定制的permalink结构,如下所示: /%category%/%postname%/ Category base设置为,我有几个帖子类别,但我只想为其中一个更改永久链接结构,名为Blog 因此,我知道我需要重写该类别下帖子的URL,我正在使用: add_filter('post_link', 'custom_permalink', 10, 3); function custom_permalink ($permalink, $post, $leavename) { //

我有一个定制的permalink结构,如下所示:

/%category%/%postname%/
Category base设置为
,我有几个帖子类别,但我只想为其中一个更改永久链接结构,名为
Blog

因此,我知道我需要重写该类别下帖子的URL,我正在使用:

add_filter('post_link', 'custom_permalink', 10, 3);

function custom_permalink ($permalink, $post, $leavename) {
    // Get the categories for the post
    $category = get_the_category($post->ID);
    if (!empty($category) && $category[0]->cat_name == "Blog") {
        $permalink = trailingslashit(home_url('/' . $post->post_name . '/'));
    }
    return $permalink;
}
因此,该链接看起来不错(URL中没有
/blog/
),但结果是

错误404

现在我想我需要设置重写规则,尝试通过以下方式实现:

add_action('generate_rewrite_rules', 'custom_rewrite_rules');

function custom_rewrite_rules($wp_rewrite) {
    // This rule will will match the post id in %postname%-%post_id% struture
    $new_rules['^/([^/]+)/?$'] = 'index.php?name=$matches[1]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}
我想我的重写规则是不正确的。你能告诉我怎么修吗

更新:

我在玩它,并达到了这一点:

function custom_rewrite_rules($wp_rewrite) {
    $new_rules['([^/]*)/?$'] = 'index.php?name=$matches[1]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}
现在所需的结构可以工作了,但我得到了分类页面的
错误
。这意味着这仍然是错误的
([^/]*)/?$
。我怎样才能将其限制为在
/
之后有内容的URL?

这些应该会帮助您


这可能就是您想要的:

function archive_rewrite_rules(){
  add_rewrite_rule(
      'blog/([^/]*)/?$',
      'index.php?post_type=post&post_name=$matches[1]',
      'top'
  );
}
add_action( 'init', 'archive_rewrite_rules' );
blog/
作为正则表达式的一部分应该限制此重写规则仅应用于blog类别


重写永久链接可能会受到永久链接设置的影响

Thnx的链接,但这是可能的吗?我已经修改了代码(look update),但从逻辑上讲:现在博客类别文章的格式是website.com/post-name/,但类别的格式是website.com/category-name/。因此,要重写,我需要捕获一个帖子名并执行该工作,但它也捕获类别名并尝试重写规则以使其生效。我想知道是否有办法把它分开,或者做“如果类别”检查。。。