2012-04-27 10 views
1

私はWPで、次のURLをリライトしようとしています: http://www.example.com/?compra=arriendo&propertytype=apartamentos&location=bogota&habitaciones=2-habitacionesWordPressのURLリライト - オプションの価値

へ: http://www.viveya.co/arriendo/apartamentos/bogota/2-habitaciones

これは私のコードです:

関数eg_add_rewrite_rules(){ グローバル$ wp_rewrite;

$new_rules = array(
    '(.+)/(.+)/(.+)/(.*)/?$' => 'index.php?compra=' . $wp_rewrite->preg_index(1) . '&propertytype=' . 

$ wp_rewrite-> preg_index(2)。 '&場所='。 $ wp_rewrite-> preg_index(3) 。 '& habitaciones ='。 $ wp_rewrite-> preg_index(4) ); $ wp_rewrite-> rules = $ new_rules + $ wp_rewrite-> rules;

}

add_action( 'generate_rewrite_rules'、 'eg_add_rewrite_rules')。

今、私はhabitacionesをオプションにします。したがって、次のURLを入力した場合: http://www.viveya.co/arriendo/apartamentos/bogota/

これはまだ動作します。 (元のURLは& habitaciones =)になります。

habitacionesが空の場合、私のコードは機能しません。なぜ私は考えていない。私の正規表現で何が間違っていますか?

ありがとうございます! Adam

+0

[WPSE](http://wordpress.stackexchange.com/)にこれを投稿しなかった特別な理由はありますか? – scribu

答えて

1

これは正規表現で解決できるものではありません。

PHPを使用してURLセグメントを解析する必要があります。

$segments = explode('/', $url); 

$query = array(); 

while ($segments) { 
    $part = array_shift($segments); 

    if (in_array($part, array('taxonomy1', 'taxonomy2', ...)) { 
    $query[ $part ] = array_shift($segments); 
    } 
} 

編集:コンセプトのテストされていない証明

function eg_add_rewrite_rules() { 
    global $wp_rewrite; 

    $new_rules = array(
     'event/(industry|location)/(.+)/(industry|location)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4), 
     'event/(industry|location)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) 
    ); 
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; 
} 
add_action('generate_rewrite_rules', 'eg_add_rewrite_rules'); 

出典:http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/

まあ、私はあなたにも、正規表現を使用することができますが、各オプションの値のための追加的な書き換えルールが必要になりますと仮定
+0

ありがとうございました、それぞれのオプションの値に対して追加の書き換えルールを追加しました!! :) – AdamGold