是否使用3个或更少匹配项添加_重写_规则?

时间:2014-12-02 作者:patrickzdb

我已经阅读了很多关于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/

    我试着再添加两组add_rewrite_rules、 像这样:

    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. 谢谢

1 个回复
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规则上的参数。很可能是某种我不知道的神秘魔法。不过,我很想知道这里到底发生了什么。

结束

相关推荐

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-