Wordpress Url重写不起作用

时间:2013-10-30 18:20:53

标签: wordpress

我正在使用Wordpress 3.5。我创建了一个名为tpage的页面。我想传递像tpage / param1这样的查询字符串。为此,我在functions.php中粘贴了以下代码,但它无法正常工作

   add_rewrite_tag('%var1%','([^&]+)');`
   add_rewrite_rule('^tpage/([^&]+)/?','index.php?page_id=4&var1=$matches[1]','top');
   //Ensure the $wp_rewrite global is loaded
   global $wp_rewrite;
    //Call flush_rules() as a method of the $wp_rewrite object
   $wp_rewrite->flush_rules();

我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

对于一个名叫Kyle E的人来说,这是一个伟大的课程,为你完成所有繁重的工作......你必须使用get_query_var('Your-Var');来获取变量。 $ _GET不起作用。祝你好运!

<?php
/*
//Author Kyle E Gentile
//To use this class you must first include the file.  
//After including the file, you need to create an options array.  For example:
$options = array(
    'query_vars' => array('var1', 'var2'),
    'rules' => array('(.+?)/(.+?)/(.+?)/?$' => 'index.php?pagename=$matches[1]&var1=$matches[2]&var2=$matches[3]')
);
//After creating our $option array, 
//we will need to create a new instance of the class as below:
$rewrite = new Add_rewrite_rules($options);
//You must pass the options array, this way. (If you don't there could be problems) 
//Then you can call the filters and action functions as below:
add_action('wp_head', array(&$rewrite, 'flush_rules'));
add_action( 'generate_rewrite_rules', array(&$rewrite, 'add_rewrite_rules') );
add_filter( 'query_vars', array(&$rewrite, 'add_query_vars') );
//That is it.
*/

//prevent duplicate loading of the class if you are using this in multiply plugins
if(!class_exists('add_rewrite_rules')){

    class Add_rewrite_rules{

        var $query_vars;
        var $rules;

        function __construct($options){
            $this->init($options);
        }

        function init($options){
            foreach($options as $key => $value){
                $this->$key = $value;
            }
        }

        function rules_exist(){
            global $wp_rewrite;

            $has_rules = TRUE;

            foreach($this->rules as $key => $value){
                if(!in_array($value, $wp_rewrite->rules)){
                    $has_rules = FALSE;
                }   
            }

            return $has_rules;
        }

        //to be used add_action with the hook 'wp_head'
        //flushing rewrite rules is labor intense so we better test to see if our rules exist first
        //if the rules don't exist flush its like after a night of drinking  
        function flush_rules(){
            global $wp_rewrite;

            if(!$this->rules_exist()){
                //echo "flushed"; // If want to see this in action uncomment this line and remove this text and you will see it flushed before your eyes
                $wp_rewrite->flush_rules();
            }
        }

        //filter function to be used with add_filter() with the hook "query_vars"
        function add_query_vars($query_vars){

            foreach($this->query_vars as $var){
                $query_vars[] = $var;
            }

            return $query_vars;
        }

        //to be used with a the add_action() with the hook "generate_rewrite_rules"
        function add_rewrite_rules(){
            global $wp_rewrite;

            $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
        }

    }

}
?>