我正在尝试为wordpress编写一个插件,但是我遇到了一些关于wp_rewrite功能的问题.
我希望通过URL传递变量使一个页面显示为多个页面(例如:www.mysite.com/wordpress?variable = helloall)
www.mysite.com/wordpress/helloall
然后我希望能够使用slug并使用搜索我的数据库. (就像我使用$_GET一样,如果我使用的是我先提到的一般方法)
我在网上找到了一些教程,但仍然可以使这个工作.我相信我的问题是由于您如何正确编写规则而缺乏理解.
我使用过这个教程:
http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html
我试图在大多数情况下使用相同的代码.我能够制定规则,但他们似乎并不想为我工作
谁能告诉我正确的格式才能做到这一点?
编辑
这是我目前的职能
function add_rewrite_rules( $wp_rewrite ) { $new_rules = array ( '(.?.+?)/(.+?)/page/?([0-9]{1,})/?$' => 'index.PHP?pagename='. $wp_rewrite->preg_index(1).'&varname='. $wp_rewrite->preg_index(2).'&page='. $wp_rewrite->preg_index(3),'(.?.+?)/(.*?)/?$' => 'index.PHP?pagename='. $wp_rewrite->preg_index(1).'&varname='. $wp_rewrite->preg_index(2) ); // Always add your rules to the top,to make sure your rules have priority $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; }
add_action(‘generate_rewrite_rules’,’add_rewrite_rules’);
解
我已经弄明白了,我打算发布这个作为答案,但似乎我现在无法回答我自己的问题,所以我正在编辑原帖:
首先,我上面发布的代码是正确的,但是它不起作用的原因是因为我没有刷新规则,我使用以下代码执行此操作:
function ebi_flush_rewrite_rules() { global $wp_rewrite; $wp_rewrite->flush_rules(); } add_action( 'init','flush_rewrite_rules');
我的新问题是,我的代码工作得很好,重定向所有页面而不是我想要的那个页面,这意味着没有子页面会显示哪个有点问题,但是我用一个小问题解决了这个问题.编辑:
function add_rewrite_rules( $wp_rewrite ) { $new_rules = array ( '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.PHP?pagename='. $wp_rewrite->preg_index(1).'&varname='. $wp_rewrite->preg_index(2).'&page='. $wp_rewrite->preg_index(3),'(testpage)/(.*?)/?$' => 'index.PHP?pagename='. $wp_rewrite->preg_index(1).'&varname='. $wp_rewrite->preg_index(2) ); // Always add your rules to the top,to make sure your rules have priority $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; }
function add_rewrite_rules( $wp_rewrite ) { $new_rules = array ( '(testpage)/(.+?)/page/?([0-9]{1,'(testpage)/(.*?)/?$' => 'index.PHP?pagename='. $wp_rewrite->preg_index(1).'&varname='. $wp_rewrite->preg_index(2) ); // Always add your rules to the top,to make sure your rules have priority $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } function query_vars($public_query_vars) { $public_query_vars[] = "varname"; return $public_query_vars; } function ebi_flush_rewrite_rules() { global $wp_rewrite; $wp_rewrite->flush_rules(); } add_action( 'init','flush_rewrite_rules'); add_action('generate_rewrite_rules','add_rewrite_rules'); add_filter('query_vars','query_vars');
我希望将来可以节省别人的时间.
我想出了如何让它在特定页面名称上运行,这适用于将来遇到问题的任何人:
原文链接:/php/134463.htmlfunction add_rewrite_rules( $wp_rewrite ) { $new_rules = array ( '(testpage)/(.+?)/page/?([0-9]{1,'(testpage)/(.*?)/?$' => 'index.PHP?pagename='. $wp_rewrite->preg_index(1).'&varname='. $wp_rewrite->preg_index(2) ); // Always add your rules to the top,to make sure your rules have priority $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } function query_vars($public_query_vars) { $public_query_vars[] = "varname"; return $public_query_vars; } function ebi_flush_rewrite_rules() { global $wp_rewrite; $wp_rewrite->flush_rules(); } add_action( 'init','query_vars');