可选重写规则中的所有捕获组

时间:2016-09-16 作者:Marko

我已经创建了页面图像。php,在该文件中,我有一些带有模板名称的代码,在仪表板中,我创建了页面Slike并选择了该模板。。。所有这些都在起作用。

正如这里描述的那样,我添加了重写规则,并刷新了永久链接https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

function pl_add_rewrite_rule() {
  add_rewrite_rule(\'^slike/([^/]*)/([^/]*)/?\',\'index.php?page_id=45893&carstvo=$matches[1]&kategorija=$matches[2]\',\'top\');
}
add_action(\'init\', \'pl_add_rewrite_rule\', 10, 0);
现在这些URL可以工作了:

但这不是

法典中规定,捕获组是可选的。我必须做些别的事情才能让它工作吗?我想有所有3种URL工作

$regex
(string) (required) A regular expression to match against a requested URL.
 This can optionally use one or more capture groups.

1 个回复
SO网友:Marko

好的,4个小时后我成功了

function pl_add_rewrite_rule() {
    // this works with pagination too
    add_rewrite_rule(\'^slike(\\/([a-z]+))?(\\/([a-z]+))?(\\/page\\/([0-9]+))?$\',\'index.php?page_id=45893&carstvo=$matches[2]&kategorija=$matches[4]&paged=$matches[6]\',\'top\');

    add_rewrite_tag(\'%carstvo%\',\'([^/]*)\');
    add_rewrite_tag(\'%kategorija%\',\'([^/]*)\');
}
add_action(\'init\', \'pl_add_rewrite_rule\', 10, 0);

相关推荐