我已经阅读了很多关于here和Google在WordPress和add_rewrite_rule
. 我发现Jan Fabry\'s answer 特别有用,但我现在有点卡住了。
我使用以下代码在URL中启用3个匹配项:
add_action( \'init\', \'wpa5413_init\' );
function wpa5413_init()
{
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/([^/]*)/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]&our_work_post_slug=$matches[3]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
}
add_filter( \'query_vars\', \'wpa5413_query_vars\' );
function wpa5413_query_vars( $query_vars )
{
$query_vars[] = \'our_year_tax_slug\';
$query_vars[] = \'our_quarter_tax_slug\';
$query_vars[] = \'our_work_post_slug\';
return $query_vars;
}
这与简·法布里的回答略有不同。我正在使用
([^/]*)
而不是
([^/]+)
虽然我发现使用两者对我的结果都没有影响。我还在子页面上使用它,所以我在初始正则表达式匹配中包含了父页面,并且
pagename=
价值
它在指定的所有3个值下都能很好地工作。在模板中使用以下代码时:
<?php
echo get_query_var( \'our_year_tax_slug\' );
echo "<br>";
echo get_query_var( \'our_quarter_tax_slug\' );
echo "<br>";
echo get_query_var( \'our_work_post_slug\' );
?>
并访问URL,如
test.dev/our-work/our-year/one/two/three/
. 我明白了
/one/two/three/
打印在页面上。问题是,我也希望这些能够起作用:
test.dev/our-work/our-year/one/two/
test.dev/our-work/our-year/one/
然而,如果我尝试访问这些URL,我会得到404。我试着再添加两组add_rewrite_rule
s、 像这样:
add_action( \'init\', \'wpa5413_init\' );
function wpa5413_init()
{
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/([^/]*)/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]&our_work_post_slug=$matches[3]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
}
但这并不奏效。我现在只能访问第一个查询变量(our_year_tax_slug
) 尽管test.dev/our-work/our-year/one/two/three
, test.dev/our-work/our-year/one/two/
和test.dev/our-work/our-year/one/
所有操作都没有404错误,模板代码只打印出第一个值one
, 无论访问哪个URL。我相信以前一定有人遇到过这个问题,如果有任何帮助,我们将不胜感激。每次更新functions.php file
. 谢谢
SO网友:patrickzdb
嗯,很明显,大量的尝试和错误都是值得的!
我使用了这个代码,在这里我颠倒了add_rewrite_rule
现在它工作得很好。
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/([^/]*)/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]&our_work_post_slug=$matches[3]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
\'our-work/our-year/([^/]*)/?\',
// The resulting internal URL: `index.php` because we still use WordPress
// `pagename` because we use this WordPress page
// `designer_slug` because we assign the first captured regex part to this variable
\'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]\',
// This is a rather specific URL, so we add it to the top of the list
// Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
\'top\' );
我猜这和
\'top\'
add\\u rewrite\\u规则上的参数。很可能是某种我不知道的神秘魔法。不过,我很想知道这里到底发生了什么。