我有以下代码:
<?php
function ex_rewrite_rules(){
//discography query variable rewrite
add_rewrite_rule( \'artists/([^/]+)/discography/?$\', \'index.php?album_artist=$matches[1]&discography=yes\', \'top\');
//albums rewrite rule
add_rewrite_rule( \'artists/([^/]+)/discography/([^/]+)/?$\', \'index.php?album_artist=$matches[1]&discography=yes&albums=$matches[2]\', \'top\' );
//songs rewrite rule
add_rewrite_rule( \'artists/([^/]+)/discography/([^/]+)/([^/]+)/?$\', \'index.php?album_artist=$matches[1]&discography=yes&albums=$matches[2]&songs=$matches[3]\', \'top\' );
}
add_action(\'init\',\'ex_rewrite_rules\');
//Function to register query vars
function ex_prefix_artists_query_var($vars){
$vars[] = \'discography\';
$vars[] = \'album_artist\';
return $vars;
}
add_filter(\'query_vars\',\'ex_prefix_artists_query_var\');
//function to load template when discography variable is called
add_filter( \'template_include\', function( $path ) {
if( get_query_var( \'discography\' ) && is_singular(\'artists\') ) {
return get_template_directory() . \'/single-artists-discography.php\';
}
return $path;
});
?>
URL重写工作正常,但discography除外,它应该加载模板的单艺术家discography。调用discography变量时使用php。相反,它将返回索引。php。换句话说,url示例。com/artists/aliciakeys/discography应该加载模板,而不是返回到站点的索引。php
我做错了什么?我怀疑这是url重写规则。
为了获得更多信息,我附上了重写分析器的屏幕截图,显示它正在正确构建
最合适的回答,由SO网友:nelson 整理而成
为了使discography规则起作用,我将规则更改为:
add_rewrite_rule( \'artists/([^/]+)/discography/?$\', \'index.php?album_artist=$matches[1]&discography=yes\', \'top\');
对此:
add_rewrite_rule( \'artists/([^/]+)/discography/?$\', \'index.php?artists=$matches[1]&discography=yes\', \'top\');
原始规则所做的是指向艺术家的名字,而不是艺术家自定义的帖子类型,因此无法重定向到模板。非常感谢@RachelCarden。