I’ve been looking for the answer to this everywhere but with no success and that’s why I’m asking here.

I don’t even know if the question is the right one.

In any case, I’ll try to explain.

I have a WordPress page with variables in the URL, like this: http://planeta-beisbol.com/tal-dia-como-hoy/?dia=18&mes=04

The content changes depending on the day and month of the year because is gathered from the databse.

My question is: How can I change the URL to look something like planeta-beisbol.com/tal-dia-como-hoy/dia/18/mes/04 or planeta-beisbol.com/tal-dia-como-hoy/18/04

Any help is apreciated. Thanks.

3 Answers
3

The following two functions should point you in the right direction:

function wpse49393_query_vars($query_vars)
{
    $query_vars[] = 'myvar';
    return $query_vars;
}
add_filter('query_vars', 'wpse49393_query_vars');

function wpse49393_rewrite_rules_array($rewrite_rules)
{
    $rewrite_rules['path/to/page/([^/]+)/?$'] = 'index.php?pagename=path/to/page&myvar=$matches[1]';
    return $rewrite_rules;
}
add_filter('rewrite_rules_array', 'wpse49393_rewrite_rules_array');

I would give credit where it is due, but I cannot remember where I originally found this code. I just pulled this out of a project that I am currently working on. To use it, change myvar to the name of your variable in the first function as well as here: &myvar=$matches[1]. Change path/to/page/ to the actual path of your page at two locations in the second function.

Leave a Reply

Your email address will not be published. Required fields are marked *