简体   繁体   English

WordPress的add_rewrite_rule

[英]Wordpress add_rewrite_rule

I'm having a strange issue trying to add a rewrite rule. 我在尝试添加重写规则时遇到了一个奇怪的问题。

Firstly, a note about my set-up. 首先,关于我的设置的注释。 I'm using a date.php file for another specific set-up on the site. 我正在使用date.php文件进行网站上的其他特定设置。

The area I'm having issues with is a customized Archives page. 我遇到的问题是自定义的“存档”页面。 In order to not mess with the date.php template I attempted to set up a new page within wordpress and then created my own template to display the archives. 为了不打扰date.php模板,我尝试在wordpress中设置一个新页面,然后创建自己的模板以显示档案。

In order to catch the url with the date and not redirect to the date.php I set up the following add_rewrite_rules : 为了捕获带有日期的URL而不重定向到date.php,我设置了以下add_rewrite_rules:

function my_archives() {

    add_rewrite_rule("my-archives/([0-9]{4})/([0-9]{1,2})/page/([0-9]{1,})/?$", 'index.php?pagename=my-archives&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]', 'top');

    add_rewrite_rule("my-archives/([0-9]{4})/([0-9]{1,2})/?$", 'index.php?pagename=my-archives&year=$matches[1]&monthnum=$matches[2]', 'top');    

}

add_action('init', 'my_archives');  

I thought everything was a-ok as my links were working correctly. 我以为一切正常,因为我的链接正常工作。 Example: http://mysite.com/my-archives/2013/09/ as well as http://mysite.com/my-archives/2013/09/page/2/ for paged results. 例如: http : //mysite.com/my-archives/2013/09/以及http://mysite.com/my-archives/2013/09/page/2/以获得分页结果。

Now a month later I see that for some reason it'll only correctly redirect to dates that are one month behind the current date. 一个月后的现在,我看到由于某种原因,它只能正确地重定向到比当前日期晚一个月的日期。 So being that the current date is 2013/10/ it's only redirecting /2013/09/ and everything else is being redirected to the 404 page. 因此,当前日期为2013/10/2013,因此仅重定向/ 2013/09 /,其他所有内容均被重定向到404页面。

Any suggestions on probable cause for this or if someone sees something in my code that is incorrect it'd be greatly appreciated. 关于可能原因的任何建议,或者有人在我的代码中看到不正确的内容,将不胜感激。

Are you by any chance simply not having any results for that query? 您是否有机会根本没有该查询的任何结果?

Are you hooking to the WordPress query in order to make sure that WordPress understands the query correctly(it's possible that WordPress is trying to find a page with slug "my-archives" within the specified year and month - and when it can't it shows a 404 error). 您是否要连接WordPress查询以确保WordPress正确理解了该查询(WordPress可能会尝试在指定的年月中查找带有“ my-archives”字样的页面-何时无法找到该页面?显示404错误)。

Try doing the following: 尝试执行以下操作:

var_dump( get_posts( 'pagename=my-archives&year=2013&monthnum=10' ) );

and see if that returns any results. 看看是否返回任何结果。 If it doesn't - then that's most-likely your issue. 如果不是,那很可能就是您的问题。

To work-around that, I would advise that you change your rewrite rules to something like: 要解决此问题,我建议您将重写规则更改为:

add_rewrite_rule("my-archives/([0-9]{4})/([0-9]{1,2})/?$", 'index.php?pagename=my-archives&my_year=$matches[1]&my_monthnum=$matches[2]', 'top');

Since I assume that if you're not hooking to the main WordPress query, you are executing the query on your custom page template. 因为我假设如果您不连接到主要的WordPress查询,那么您将在自定义页面模板上执行查询。 There you should look for the my_year and my_monthnum query variables instead of year and month . 在那里,您应该查找my_yearmy_monthnum查询变量,而不是yearmonth

Keep in mind, that actually modifying the main WordPress query(on the either parse_request (passes the $wp object and not $wp_query ) or the pre_get_posts action(passes the $wp_query object) ) might be a better solution. 请记住,实际上修改主WordPress查询(在parse_request (通过$wp对象而不是$wp_query )或pre_get_posts操作(通过$wp_query对象)上)可能是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM