简体   繁体   中英

how to route with variables in wordpress url?

I have a wordpress theme template page accessible through "www.myDomain.com/documents". The slug of the page is therefor "documents".

But i can't seem to find a way to add a ressource to the URL. How can i add a ressource like "123" at the end and still have it go to my "documents" page?

Like so "www.myDomain.com/documents/123" with the last part being the id to get in my wordpress theme page code... right now it just ends up as a 404.

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'documents/([A-Za-z0-9_-]+)/?$',
        'index.php?pagename=documents&documents_id=$matches[1]',
        'top' );
}

add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
    $query_vars[] = 'documents_id';
    return $query_vars;
}

add_filter('init','flushRules');
function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM