简体   繁体   English

wordpress将参数传递给自定义模板

[英]wordpress passing parameter to custom template

I am using wordpress. 我正在使用wordpress。 I have created a page dealer-profile from admin and assigned a template to it. 我从管理员创建了一个页面经销商配置文件并为其分配了一个模板。 Now I want to pass a parameter like 现在我想传递一个参数

SITE-URL/dealer-profile/SUV

I have added following into my .htaccess 我在我的.htaccess添加了以下内容

RewriteRule ^dealer-profile/([a-zA-Z0-9_-]+)(|/)$ index.php?pagename=dealer-profile&dealer=$1 [QSA]

I have also tried following 我也尝试过以下

add_rewrite_rule('dealer-profile/([^/]+)', 'index.php?pagename=dealer- profile&dealer=$matches[1]', 'top');
flush_rewrite_rules(false);

when I am requesting SITE-URL/dealer-profile/SUV , it automatically redirects to SITE-URL/dealer-profile 当我申请SITE-URL/dealer-profile/SUV ,它会自动重定向到SITE-URL/dealer-profile

Please suggest, where I am wrong. 请建议,我错了。

Now I am doing following 现在我正在做以下事情

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');

But this is working for digits only like - test/dealer-profile/234234/ is working - test/dealer-profile/234234/ is not working 但这只适用于数字 - test / dealer-profile / 234234 /正在运行 - test / dealer-profile / 234234 /无效

Solution

Ref : http://codex.wordpress.org/Class_Reference/WP_Rewrite 参考: 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;
}

Thanks A lot for Your Help @@jnhghy - Jantea Alexandri 非常感谢你的帮助@@ jnhghy - Jantea Alexandri

You need to create a function to add query variables to query string and then hook that function into the query_vars hook: 您需要创建一个函数来将查询变量添加到查询字符串,然后将该函数挂钩到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');

then we need to add the rewrite rule: function add_rewrite_rules($aRules) { 然后我们需要添加重写规则: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');

Getting the variable: 获取变量:

if(isset($wp_query->query_vars['dealer-profile'])) {
$sMsdsCat = urldecode($wp_query->query_vars['dealer-profile']);
}

and this should do the trick, and here are some extra link for "advanced rules": 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 这应该是诀窍,这里有一些额外的“高级规则”链接: 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

EDIT: let us try the steps that we have in the second article: First let's create the tag and add it to the rewrite rules (this should work for alpha-numeric values: 编辑:让我们尝试第二篇文章中的步骤:首先让我们创建标记并将其添加到重写规则中(这应该适用于字母数字值:

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');

Then hook generate rules like this: 然后钩子生成这样的规则:

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');

add the variable to query vars: 将变量添加到查询变量:

function query_vars($public_query_vars) {
    $public_query_vars[] = "dealer-profile";
    return $public_query_vars;
add_filter('query_vars', 'query_vars');

bonus: To debug your rules you can use this function: 奖励:要调试规则,您可以使用此功能:

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>';
}

it's from https://docs.dev4press.com/tutorial/wordpress/debug-wordpress-rewrite-rules-matching/ and it should show you all your rules in a table format, just include it in any of your templates and check if the above functions are adding the rewrite rules correctly if not, it should give you some extra info about what didn't worked. 它来自https://docs.dev4press.com/tutorial/wordpress/debug-wordpress-rewrite-rules-matching/ ,它应该以表格格式显示您的所有规则,只需将其包含在您的任何模板中并检查是否如果没有正确添加重写规则,它应该给你一些关于什么没有用的额外信息。

And you can also use the well known Developer plugin to inspect your rules, you'll get extra info with this plugin!!! 而且您还可以使用众所周知的Developer插件来检查您的规则,您将获得此插件的额外信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM