我正在尝试为WordPress编写一个插件,但是我遇到了一些关于wp_rewrite功能的问题。
我想通过URL传递变量,使一个页面显示为多个页面(例如:www.mysite.com/WordPress?variable = helloall)
但是我想保持固定链接结构不变,所以我希望URL显示为:
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;
}
所以关于wp_rewrite功能的最终代码如下:
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 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');
我希望将来可以节省别人的时间。
答案 0 :(得分:6)
我想出了如何使用特定页面名称,这适用于将来遇到问题的任何人:
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 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');