我试图根据不同的分类法列出附件。目前它只适用于基本url(未添加分页)。例如
http://www.example.com/exams/high-school/city-name/subject/math/
首先,我在函数中添加了自定义重写规则。php如下:
function add_rewrite_rules($aRules) {
$new_rules = array(
// rule 1
\'exams/([^/]+)/([^/]+)/subject/([^/]+)/?$\' =>
\'index.php?pagename=exams&level=$matches[1]&city=$matches[2]&subject=$matches[3]\',
// rule 2
\'exams/([^/]+)/([^/]+)/subject/([^/]+)/page/([0-9]+)/?$\' =>
\'index.php?pagename=exams&level=$matches[1]&city=$matches[2]&subject=$matches[3]&page=$matches[4]\'
);
return $new_rules + $aRules;
}
// hook add_rewrite_rules function into rewrite_rules_array
add_filter(\'rewrite_rules_array\', \'add_rewrite_rules\');
每次我更改功能时,我都会转到wordpress、永久链接并单击save。
其次,我在URL中添加了我正在使用的查询变量(也在functions.php中):
// represents for example the name of the product category
function add_query_vars($aVars) {
$aVars[] = "level";
$aVars[] = "city";
$aVars[] = "subject";
$aVars[] = "page";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter(\'query_vars\', \'add_query_vars\');
第三,我创建了一个名为“考试”的页面和一个插件,以获取只有在您正在观看的页面是考试时才能使用的附件:
// Hooks a function to a filter action, \'the_content\' being the action...
add_filter(\'the_content\',\'me_get_listado\');
// Callback function
function me_get_listado($content)
{
// Checking if on post page.
if ( is_page(\'exams\') )
{
// get the query vars
if ( get_query_var( \'level\' ) && get_query_var( \'city\' ) && get_query_var( \'subject\' ) )
{
// get_post ($args) ...
}
return $content; // the new content
} else {
// else on blog page / home page etc, just return content as usual.
return $content;
}
}
嗯,这几乎和预期的一样。当我去
http://www.example.com/exams/high-school/city-name/subject/math/
我从“城市名称”中得到了“高中”的“数学”附件。
But when I go to: http://www.example.com/exams/high-school/city-name/subject/math/page/1
or: http://www.example.com/exams/high-school/city-name/subject/math/page/2
I\'m redirected to:
http://www.example.com/exams/1 or http://www.example.com/exams/2
所以,什么都没用。
我想知道我添加的重写规则有什么问题?或者如果我干扰了Wordpress的逻辑?
注意:“级别”、“城市”和“主题”是自定义分类法。
最合适的回答,由SO网友:pQB 整理而成
根据@Milo的评论并使用重写分析器插件,我发现重写规则中存在冲突?。以下url:
exams/high-school/city-name/subject/math/
符合以下规则
Pattern:
(.?.+?)/page/?([0-9]{1,})/?$
Subtitution:
pagename: exams/high-school/city-name/subject/math
paged: 1
Pattern:
(.?.+?)(/[0-9]+)?/?$
Subtitution:
pagename: exams/high-school/city-name/subject/math/page
page: /1
Pattern:
exams/([^/]+)/([^/]+)/subject/([^/]+)/?$
Subtitution:
pagename: examns
level: high-school
city: city-name
subject: math
page: 1
最后一个是我正在尝试创建的。我不是重写规则方面的专家,但我认为第二个规则是首先激活的,并产生错误的重定向到
http://www.example.com/exams/1 or http://www.example.com/exams/2
.
因此,我将第二条规则改为:
exams/([^/]+)/([^/]+)/subject/([^/]+)/page/?([0-9]{1,})/?$\' => \'index.php?pagename=exams&level=$matches[1]&city=$matches[2]&subject=$matches[3]&pages=$matches[4]
将var记为\'
pages\' 复数形式。它工作正常。但是,重写分析器插件没有检测到我的规则。“?”
事实上,如果我将“page”改为“pages”或“pages”whateverIwant\' 然后注册该varname并将其放入我的插件中:
$args = array( \'post_type\' => \'attachment\',
\'posts_per_page\' => 2,
\'paged\' => ( get_query_var(\'whateverIwant\') ? get_query_var(\'whateverIwant\') : 1 ),
\'orderby\' => \'post_title\',
\'order\' => \'ASC\',
\'post_status\' =>\'published\'
);
同样有效!。因此,我创建的第一组规则与wordpress内部的某些内容相冲突。我有很多东西要学。
附言:我会接受任何有坚实论据的问题来解决这个问题。