带有4个vars的Wordpress add_rewrite_rule无效

时间:2014-05-29 11:19:41

标签: regex wordpress url-rewriting

我正在尝试重写自定义页面模板的网址 链接http://pm.com/cusotm-page/7170-West-9th-Avenue-Lakewood-CO-80214/29415331/RES/METRO应写为

http://pm.com/cusotm-page/?title=2005-South-Monroe-Street-Denver-CO-80210&ppid=29377927&property=RES&originator=METRO

functions.php中的代码是

add_action( 'init', 'wpa5413_init' );

function wpa5413_init(){
    add_rewrite_tag('%title%','([^&]+)');
    add_rewrite_tag('%ppid%','([^&]+)');
    add_rewrite_tag('%property%','([^&]+)');
    add_rewrite_tag('%originator%','([^&]+)');

    add_rewrite_rule('^cusotm-page-homendo/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$','cusotm-page/?title=$matches[1]&ppid=$matches[2]&property=$matches[3]&originator=$matches[4]','top');
 }
你能告诉我我做错了什么吗?是正则表达式正确

PS“我刷新了重写规则,get变量添加了add_query_arg

1 个答案:

答案 0 :(得分:0)

好的,解决方案是在重写时使用带有pagename的index.php

function wpa5413_init(){

add_rewrite_tag('%title%','([^&]+)');
add_rewrite_tag('%ppid%','([^&]+)');
add_rewrite_tag('%property%','([^&]+)');
add_rewrite_tag('%originator%','([^&]+)');   
add_rewrite_rule('^cusotm-page/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$','index.php?pagename=cusotm-page&title=$matches[1]&ppid=$matches[2]&property=$matches[3]&originator=$matches[4]','top');
 }

这将重写网址,http://pm.com/cusotm-page/7170-West-9th-Avenue-Lakewood-CO-80214/29415331/RES/METRO之类的链接将变为可用。 但是$ _GET ['title']不起作用 - 但您可以使用$ wp_query-> query_vars ['title']

访问变量

还要确保在显示nice链接之前使用add_query_arg。

希望它有助于某人...