WordPress未选择自定义重写规则

时间:2012-10-28 作者:Umesh Awasthi

我正在尝试创建一个自定义重写URL

function add_my_rule() {    
    global $wp; 
    $wp->add_query_var(\'state\');   
    $wp->add_query_var(\'state_destination\');
    add_rewrite_rule(\'destination/([0-9]+)/([^/]*)/page/([0-9]+)\',\'index.php?pagename=destination&state=$matches[1]&state_destination=$matches[2]&paged=$matches[3]\',\'top\');
    add_rewrite_rule(\'destination/([0-9]+)/([^/]*)\',\'index.php?pagename=destination&state=$matches[1]&state_destination=$matches[2]\',\'top\');
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action(\'init\', \'add_my_rule\');
想法是重写所有具有以下模式的URL

www.abc.com/destination/state/state-destination
我有一个自定义模板目标。php和我希望所有这样的URL都在那里处理。我在过去也做过同样的事情,效果很好,但现在对于给定的URL来说似乎根本不起作用

http://localhost/xxx/destination/himachal-pradesh/dalhousie
它总是会single.php 而不是destination.php.我试图刷新缓存,甚至浏览器缓存,但似乎我的重写规则不正确或存在其他问题。这方面的任何帮助都将帮助我前进。

作为补充信息,我在WordPress中当前的URL结构是/%year%/%monthnum%/%postname%.html 我正在使用启用模式重写的WAMP服务器。

1 个回复
最合适的回答,由SO网友:Marty 整理而成

这是一个很好的重写类,作者Kyle E GentileI不久前发现了这个类,它对我创建的大多数插件都很有用。。。

将第一段代码保存在名为:add_rewrite_rules.php

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

    }

}
?>
然后在代码中包含该文件以生成新的重写规则。。

<?php
//-------------------------------------------------
//ADDING REWRITE RULES AND QUERY VARS
//-------------------------------------------------
include(\'add_rewrite_rules.php\');
$options = array(
    \'query_vars\' => array(\'state\', \'state_destination\'),
    \'rules\' => 
        array(
            \'(.+?)/([^/]+)/([^/]+)/?$\' => \'index.php?  pagename=$matches[1]&state=$matches[2]&state_destination=$matches[3]\'
        )
);
    $rewrite = new Add_rewrite_rules($options);
    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\') );
   //-------------------------------------------------
   //ADDING REWRITE RULES AND QUERY VARS
   //-------------------------------------------------
   ?>
应该是这样的,就像我说的,我在构建不同插件时已经使用过几次,我希望它对你来说可以正常工作。。

马蒂

结束