我有一个分类搜索表单,链接输出是
http://localhost/wp/?cityid=16
但我想把它改写成
http://localhost/wp/cityid/16
。htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress
它不起作用:
RewirteRule ^wp/cityid/([0-9]+)$ ?cityid=$1 [L]
它也不起作用(来自wp codex):
add_action(\'generate_rewrite_rules\', \'geotags_add_rewrite_rules\');
function geotags_add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
\'cityid/(.+)\' => \'index.php?cityid=\' .
$wp_rewrite->preg_index(1) );
// Add the new rewrite rule into the top of the global rules array
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
有什么帮助吗?
最合适的回答,由SO网友:KalenGi 整理而成
请尝试以下代码:
function geotags_add_rewrite_rules($wp_rewrite_rules) {
global $wp_rewrite;
$rule_key = \'%cityid%\';
$url_pattern = \'([^/]+)\';
$query_string = \'cityid=\';
$wp_rewrite->add_rewrite_tag($rule_key, $url_pattern, $query_string);
$url_structure = $wp_rewrite->root . "cityid/$rule_key/";
$rewrite_rules = $wp_rewrite->generate_rewrite_rules($url_structure);
$wp_rewrite_rules = $rewrite_rules + $wp_rewrite_rules;
return $wp_rewrite_rules;
}
add_filter(\'rewrite_rules_array\', \'geotags_add_rewrite_rules\');
function geotags_add_query_var($query_vars) {
array_push($query_vars, \'cityid\');
return $query_vars;
}
add_filter(\'query_vars\',\'geotags_add_query_var\');
NOTE: 请记住保存永久链接,以便WP能够获取您的新规则。