我正在尝试为 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');
我希望这能在将来为其他人节省一些时间。
最佳答案
我想出了如何让它在特定页面名称上工作,这适用于将来遇到问题的任何人:
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');
关于php - Wordpress - wp_rewrite 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13140182/