我正在使用wordpress。 我从管理员创建了一个页面经销商配置文件并为其分配了一个模板。现在我想传递一个像
这样的参数SITE-URL/dealer-profile/SUV
我已在我的.htaccess
RewriteRule ^dealer-profile/([a-zA-Z0-9_-]+)(|/)$ index.php?pagename=dealer-profile&dealer=$1 [QSA]
我也试过了
add_rewrite_rule('dealer-profile/([^/]+)', 'index.php?pagename=dealer- profile&dealer=$matches[1]', 'top');
flush_rewrite_rules(false);
当我请求SITE-URL/dealer-profile/SUV
时,它会自动重定向到SITE-URL/dealer-profile
请提出建议,我错了。
现在我正在做以下
function themeslug_query_vars( $qvars ) {
$qvars[] = 'dealer';
return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
function add_rewrite_rules($aRules) {
$aNewRules = array('dealer-profile/([^/]+)/?$' => 'index.php?pagename=dealer-profile&dealer=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
但这只适用于数字 - test / dealer-profile / 234234 /正在运行 - test / dealer-profile / 234234 /无效
解决方案
参考:http://codex.wordpress.org/Class_Reference/WP_Rewrite
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(dealer-profile)/(.*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['dealer-profile/(.*)$'] = 'index.php?pagename=dealer-profile&dealer=$matches[1]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'dealer');
return $vars;
}
非常感谢你的帮助@@ jnhghy - Jantea Alexandri
答案 0 :(得分:3)
您需要创建一个函数来将查询变量添加到查询字符串,然后将该函数挂钩到query_vars钩子中:
function add_query_vars($aVars) {
$aVars[] = "dealer-profile"; // represents the name of the variable that you want to pass
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
然后我们需要添加重写规则: function add_rewrite_rules($ aRules){
function add_rewrite_rules($aRules) {
$aNewRules = array('dealer-profile/([^/]+)/?$' => 'index.php?pagename=dealer-profile=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');
获取变量:
if(isset($wp_query->query_vars['dealer-profile'])) {
$sMsdsCat = urldecode($wp_query->query_vars['dealer-profile']);
}
这应该可以解决问题, 以下是"高级规则的一些额外链接": http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html
编辑:让我们尝试第二篇文章中的步骤: 首先,让我们创建标记并将其添加到重写规则中(这应该适用于字母数字值:
function createRewriteRules() {
global $wp_rewrite;
// add rewrite tokens
$dealertag = '%dealertag%';
$wp_rewrite->add_rewrite_tag($dealertag, '(.+?)', 'dealer-profile=');
$keywords_structure = $wp_rewrite->root . "dealer-profile/$dealertag/";
$keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
$wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'createRewriteRules');
然后钩子生成这样的规则:
function add_rewrite_rules( $wp_rewrite )
{
add_rewrite_rule('^dealer-profile/([^/]*)/?','index.php?page_id=12&dealer-profile=$matches[1]','top');
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
将变量添加到查询变量:
function query_vars($public_query_vars) {
$public_query_vars[] = "dealer-profile";
return $public_query_vars;
add_filter('query_vars', 'query_vars');
奖金: 要调试规则,您可以使用此功能:
function dev4press_debug_rewrite_rules() {
global $wp_rewrite;
echo '<div>';
if (!empty($wp_rewrite->rules)) {
echo '<h5>Rewrite Rules</h5>';
echo '<table><thead><tr>';
echo '<td>Rule</td><td>Rewrite</td>';
echo '</tr></thead><tbody>';
foreach ($wp_rewrite->rules as $name => $value) {
echo '<tr><td>'.$name.'</td><td>'.$value.'</td></tr>';
}
echo '</tbody></table>';
} else {
echo 'No rules defined.';
}
echo '</div>';
}
它来自https://docs.dev4press.com/tutorial/wordpress/debug-wordpress-rewrite-rules-matching/,它应该以表格形式显示您的所有规则,只需将其包含在您的任何模板中,并检查上述功能是否正确添加重写规则,如果不是,它应该给你一些关于什么没有用的额外信息。
您还可以使用众所周知的Developer插件来检查您的规则,您将获得此插件的额外信息!!!