WordPress Custom Rewrite Rule towards Custom Page

我只是一个虾纸丫 提交于 2019-12-13 16:19:29

问题


Hi I have a wordpress based website that contains movies and tv-shows. Meta datas include the year of release, e.g. Inception has 2010 as year_of_release, the key of the meta data value. I need to do something to make a custom link so that it would be like

website.com/year/2010

and it would show a custom page like year.php or whatever and take 2010 as a GET parameter so that I can show every movie released in 2010, or 2011 or whatever the parameter is.

Basically it would be like website.com/category/sub-category but I don't want it to be a category, since the year is saved as meta data and it would be wasteful to create a subcategory for every year.

Can anyone tell me a way to make a custom rewrite rule or to make a redirect rule to force a link like that to show a custom page of mine? Thanks in advance to everyone, I hope I made myself clear.


回答1:


Add rewrite rule in functions.php

function year_rewrite() {
  add_rewrite_rule('^year/([0-9]+)/?', 'page-year.php?year=$matches[1]',    'top');
}
add_action('init', 'year_rewrite');

in your page-year.php create the loop to query the post by post meta year from the url parameter year.

Then go to Settings->Permalinks and just Save so WordPress will flush the new rules and it will work.



来源:https://stackoverflow.com/questions/37059475/wordpress-custom-rewrite-rule-towards-custom-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!