web-dev-qa-db-ja.com

クエリ文字列をかなりパーマリンクに変更

リンクがあります http://www.example.com/my-account/view-pet/?pet-page=2

どちらかに変更したい

これが私がこれまでに試みたことです

function wpse12065_init() {
    add_rewrite_rule('view-pet(/([^/]+))?(/([^/]+))?/?',
        'index.php?pagename=my-account/view-pet&pet-page=$matches[2]',
        'top');
}
add_action( 'init', array( $this, 'wpse12065_init' ) );

get_query_var( 'pet-page' )を使って?pet-page=2を取得することができます

私も THIS を見てきましたが、まだ動作していません

任意の助けは大歓迎です

2
bagpipper

functions.phpでこの書き換え規則を試してください

function string_url_rewrite() {
    global $wp_rewrite;
    add_rewrite_tag('%pet-page%', '([^&]+)');
    //In the rewrite rule you need to enter page slug and page id
    add_rewrite_rule('^Enter_Page_slug/([0-9]+)/?', 'index.php?page_id=Enter_Page_ID&pet-page=$matches[1]', 'top');

    $wp_rewrite->flush_rules(true);
}
add_action('init', 'string_url_rewrite', 10, 0);

anchor tagのurlのファーマットも変更する必要があります。たとえば、前のURL

http://www.example.com/my-account/view-pet/?pet-page=2

このURLをこれに変更

http://www.example.com/my-account/view-pet/2

pet-pageの値を取得するには、以下のメソッドを使用します。

//Storing the value of 'pet-page' in variable
$value = get_query_var( 'pet-page' );
if($value){  //checking if variable has any value or not
    echo 'This is the value '.$value;
} else {
    echo 'No Values';
}   

注:これがうまくいかない場合は、パーマリンクをフラッシュしてください。そのためにはsentting -> permalinkに行き変更を保存してください。

2
Rishabh