我通常很难做到这一点,但这次我做得相对容易。此答案的主要参考资料如下:
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
我想使用
anything
在以下内容中为
discipline
.
/artists/djave_co/anything
首先,我添加了一个“重写标记”
function custom_rewrite_tag() {
add_rewrite_tag(\'%discipline%\', \'([^&]+)\');
}
add_action(\'init\', \'custom_rewrite_tag\', 10, 0);
接下来,我做到了
print_r($wp_query->query_vars)
在默认自定义帖子页面上。结果如下:
Array
(
[page] => 0
[artists] => djave_co
[post_type] => artists
[name] => djave_co
[error] =>
...
这对我很有帮助,因为我知道我需要
post_type=artists&artists=djave_co
在下一步中。
下一步是添加自定义重写规则:
function custom_rewrite_rule() {
add_rewrite_rule(
\'^artists/([^/]*)/([^/]*)/?\',
\'index.php?post_type=artists&artists=$matches[1]&discipline=$matches[2]\',
\'top\');
}
add_action(\'init\', \'custom_rewrite_rule\', 10, 0);
决赛和
very important step was to clear the permalinks in the WordPress admin panel.