WordPress - 使额外的参数可选

时间:2016-01-18 19:17:26

标签: wordpress parameters optional

我正在尝试获取页面参数而不在WordPress中编辑.htaccess 像这样:

自:http://localhost/wordpress1/download/?image=64&resolution=1024x768&nonce=a102d152a0

于: http://localhost/wordpress1/download/64/1024x768/a102d152a0/

我的代码如下,它无效,导致404错误。

function new_rewrite_rule(){
    add_rewrite_rule(
        'download/([0-9]+)/([a-z]+)/([a-z]+)/?$',
        'index.php?pagename=download&image=$matches[1]&resolution=$matches[2]&nonce=$matches[3]',
        'top'
    );
}
add_action( 'init', 'new_rewrite_rule' );

function new_query_vars( $query_vars ) {
    $query_vars[] = 'image';
    $query_vars[] = 'resolution';
    $query_vars[] = 'nonce';

    return $query_vars;
}
add_filter( 'query_vars', 'new_query_vars' );

1 个答案:

答案 0 :(得分:0)

我解决了, 最终守则

    function new_rewrite_rule(){
    global $wp_rewrite; 
    $wp_rewrite->flush_rules(); 

    add_rewrite_rule(
        'download/([0-9]+)/([a-zA-z0-9]+)/([a-zA-z0-9]+)/?$',
        'index.php?pagename=download&image=$matches[1]&resolution=$matches[2]&nonce=$matches[3]',
        'top'
    );
}

add_action( 'init', 'new_rewrite_rule' );

function new_query_vars( $query_vars ) {
    $query_vars[] = 'image';
    $query_vars[] = 'resolution';
    $query_vars[] = 'nonce';

    return $query_vars;
}
add_filter( 'query_vars', 'new_query_vars' );