我正在尝试定制重写规则,如下所示:
add_rewrite_rule(
'sample-list/([A-Za-z0-9-]+)/?$',
'index.php?pagename=sample-list&custom_var=matches[1]',
'top'
);在sample-list中,我有一些带有链接的列表。
我也有页面样本列表,这是我生成这些链接的模板。链接看起来是这样的:http://test.local/sample-list/whatever-111,所以在这种情况下我不能访问custom_var,它是空的。
我设法解决这个问题的唯一方法是创建自定义帖子类型,使用与页面不同的名称。现在结构看起来像这样:
列表:http://test.local/sample-list (页面)
单项:http://test.local/sample-list-item/whatever-111 (sample-list-item为自定义贴子类型)
我无法创建与页面名称相同的自定义帖子类型。
所以我的目标是:
列表:http://test.local/sample-list
单项:http://test.local/sample-list/whatever-111
而custom_var将是111
发布于 2019-02-21 19:06:36
尝试使用上面的代码添加以下代码,您可以阅读完整的文档Wordpress Rewrite_API
function custom_rewrite_tag() {
add_rewrite_tag('%custom_var%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);发布于 2019-03-03 18:41:48
这个方法起作用了:
add_filter('generate_rewrite_rules', 'generate_rewrite_rules');
function generate_rewrite_rules($wp_rewrite) {
$wp_rewrite->rules = array_merge(
['sample-list/[^/]*\D(\d+)/?$' => 'index.php?some_id=$matches[1]'],
$wp_rewrite->rules
);
}https://stackoverflow.com/questions/54801821
复制相似问题