TEMPLATE_INCLUDE未加载-而是转到index.php

时间:2014-11-05 作者:nelson

我有以下代码:

<?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重写规则。

为了获得更多信息,我附上了重写分析器的屏幕截图,显示它正在正确构建rewrite analyzer screenshot for the URL rules detailed above

1 个回复
最合适的回答,由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。

结束

相关推荐

Custom permalinks structure

我希望有这样的结构:www.mysite.com/2013 (必须显示2013年的所有职位)www.mysite.com/my-category/2013 (必须显示2013年和“我的类别”类别的所有帖子)www.mysite.com/my-category/my-tag/ (必须显示所有类别为“我的类别”和标记为“我的标记”的帖子)www.mysite.com/my-category/ (必须显示“我的类别”类别的所有帖子)www.mysite.com/my-