我有一个artists-single.php
导航到时显示的页面模板:
/artists/djave_co
有没有路线/artists/djave_co/anything
到相同的artists-single.php
页然后,我可以使用一些PHP逻辑来处理页面的呈现。我有一个artists-single.php
导航到时显示的页面模板:
/artists/djave_co
有没有路线/artists/djave_co/anything
到相同的artists-single.php
页然后,我可以使用一些PHP逻辑来处理页面的呈现。我通常很难做到这一点,但这次我做得相对容易。此答案的主要参考资料如下:
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.我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。