how to pass city name in parmalink structure of wordpress

I have a static front page in WordPress and I want to use a city name in the permalink so that I can change the contents of the page on the basis of the detected city. However, when I try to add a rule, it takes me to the wrong page. Here is a sample of the code I’m trying to use:

$wp_rewrite->add_rule('^city-([^/]*)/?$','index.php?cityNameParma=$matches[1]','top');

It’s for a page which will display businesses directory listings. Upon clicking I added a new rule
which should allow inner pages to work as intended:

Read More
$wp_rewrite->add_rule('^city-([^/]*)/wpbdm-category/([^/]*)/?$','index.php?wpbdm-category=$matches[2]&cityNameParma=$matches[1]','top'); 

I then get the city name from the URL with

$wp_query->query_vars['cityNameParma'];

The only problem that remains is how to get the value when a static home page is called and how I send the parameter value as I have done for inner pages.

Kindly help me out as I have melted my brain.

Related posts

Leave a Reply

1 comment

  1. i think you also have to add your new parameter to the accepted query-params:

    function my_query_vars( $qvars )
    {
        $qvars[] = 'cityNameParma';
        return $qvars;
    }
    add_filter('query_vars', 'my_query_vars');
    

    then you can access it in your template files like you described:

    global $wp_query;
    $wp_query->query_vars['cityNameParma'];