0

I have a custom taxonomy called Area, which is hierarchical like so:

UK
 - London
   -- Chelsea

Currently my permalink is setup to rewrite like so:

'rewrite' => array('slug' => '%area%/%something%'

Which comes out as:

/Chelsea/Something

What I want is for it to come out like this:

/UK/London/Chelsea/Something

How do I go about this?

1 Answer 1

0

Add the value you want before the "meaningful" url variables (city, area, etc) to the slug like this:

"uk/([^/]*)/([^/]*)/?$" => 'index.php?city=$matches[1]&area=$matches[2]&something=$matches[3]',

This way it will only "capture" those URL variables (city, area) if "uk" is at the beginning.

A more complete example might look like this

//Set up the additional values in the "rewrite rules" array
function my_rewrites($rules){

        $new_rules = array(

            //uk/London/Chelsea/
            "uk/([^/]*)/([^/]*)/?$" => 'index.php?city=$matches[1]&area=$matches[2]',

        );

        $new_rules = array_merge($new_rules, $rules);

        return $new_rules;
}
add_filter('rewrite_rules_array', 'my_rewrites');


/**
 * Add Rewrite Tags 
 */
function my_rewrite_tags(){

    //EG "London"
    add_rewrite_tag('%city%','([^/]*)');

    //EG "Chelsea"
    add_rewrite_tag('%area%','([^/]*)');

}
add_action('init', 'my_rewrite_tags');

/**
 * Use the information provided in the URL like this:
 */
function my_custom_function_which_does_stuff(){
    global $wp_query;

    if ( isset( $wp_query->query_vars['city'] ) ){

        //EG London
        $city = $wp_query->query_vars['city'];

        //EG Chelsea
        $area = $wp_query->query_vars['area'];

    }

}
add_action( 'init', 'my_custom_function_which_does_stuff' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.