- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试匹配以下网址:
planets/animals-and-more/animal/human
收件人:
index.php?pagename=animal&animal-name=human
页面“animal”有一个自定义的 page-animal.php 模板,该模板拉入“animal-name”查询变量并将其吐入 .
我已经设置了重写标签:
function animal_custom_rewrite_tag() {
add_rewrite_tag('%animal-name%', '([^&]+)');
}
add_action('init', 'animal_custom_rewrite_tag', 10, 0);
重写规则:
function add_some_rewrite_rules() {
global $wp_rewrite;
add_rewrite_rule('^planets/animals-and-more/animal/[a-zA-Z0-9]*$','index.php?pagename=animal&animal-name=$matches[4]','top');
$wp_rewrite->flush_rules();
}
add_action( 'init' , 'add_custom_ncta_rewrite_rules' );
如果我将字符串硬编码为“animal-name=monkey”,重写规则会起作用,但似乎 matches[n] 数组根本不起作用。
我知道这一切都可以通过修改 htaccess 来实现,但我需要一个适用于 Wordpress 的解决方案。
知道为什么 $matches[] 根本不起作用吗?
最佳答案
找到了!
This article from theme.fm提到 matches[n] 数组仅适用于正则表达式中被括号包围的组。例如:
获取 $matches 1要在转到“行星/动物及更多/动物/人类”时返回“人类”,重写规则需要如下所示:
add_rewrite_rule('^planets/animals-and-more/animal/([a-zA-Z0-9-_]+)$','index.php?pagename=sponsorship&sponsorship_item=$matches[1]','top');
用 () 围绕您要“匹配”的组。
关于php - Wordpress add_rewrite_rule 不返回 $matches[] 数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25166434/
我正在尝试使用 wordpress 的 add_rewrite_rule 函数添加自定义规则,但它们不起作用。奇怪的是,每当我使用 检查规则时,都会添加规则并匹配我的 URL。 Monkeyman 重
目标 一个可见的 URL: /products/building-automation/details/productSKU 应该指向真实的 WordPress 页面: /products/detai
我想更改特定自定义帖子类型的永久链接结构。即 http://test.com/brand/calvin-klien?mtype=tab2 ^this is d
我可以得到 add_rewrite_rule使用 MAMP 在本地工作,但不在我的 DreamHost 生产服务器上工作。 我正在尝试美化个人宠物的网址 http://www.pawsnewengla
我是 Stack Overflow 的新手。 我正在使用 Wordpress 3.9,我正在尝试使用以下方法重写 URL,该方法位于 functions.php 中: 我用这个方法: add_rewr
我有一个名为“People”的页面,其中包含一个自定义模板,该模板接受一个名为 name 的变量: example.com/people/?name=FirstLast 我想要的是这个: exampl
我正在尝试匹配以下网址: planets/animals-and-more/animal/human 收件人: index.php?pagename=animal&animal-name=human
我正在尝试执行以下操作: 重写我的 WordPress 安装的 URL 结构,以便在其中包含语言字段。例如http://www.mydomain.com/lang/ 然后我想从/lang/获取输入并使
我是一名优秀的程序员,十分优秀!