WordPress对自定义页面的自定义重写规则 [英] WordPress Custom Rewrite Rule towards Custom Page

查看:199
本文介绍了WordPress对自定义页面的自定义重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于wordpress的网站,其中包含电影和电视节目。元数据包括发布年份,例如初始将2010年作为year_of_release,即元数据值的键。我需要做一些事情来创建自定义链接,以便它像

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

,它将显示一个自定义页面,例如 year.php 或其他内容,并收取 2010 作为GET参数,这样我就可以显示2010年或2011年或该参数发布的所有电影。

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.

基本上例如 website.com/category/sub-category ,但我不希望它成为类别,因为该年份保存为元数据,创建起来很浪费

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.

推荐答案

在functions.php中添加重写规则

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');

在您的page-year.php中,创建循环以从网址按帖子元年份查询帖子参数年。

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

然后转到设置->永久链接,然后单击保存,因此WordPress将刷新新规则并生效。

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

这篇关于WordPress对自定义页面的自定义重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆