Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a page with slug/name charts. So the url looks like http://domain.tld/charts/ for the Charts page. When i type the url to http://domain.tld/charts/(something) or http://domain.tld/charts/(something)/(something), i got error not found, logically it should as there is no sub page with the given names.

Now what is want is, when is visit to http://domain.tld/charts/(something) or http://domain.tld/charts/(something)/(something), it should still query the Charts page, and also add the single or multiple occurred (something) into wp query var so that i can access it with get_query_var function. The (something) would return as an array ofcourse.

The (something) could be any Alphanumeric characters, and not needed to be a valid one. Just need it so that WordPress Still makes query for the Chart page, and all the extra url path are added to wp_query Object.

Your help is strongly needed.

share|improve this question
1  
Have a look at add_rewrite_rule questions and in Codex. –  Milo Dec 31 '13 at 0:29

1 Answer 1

up vote 0 down vote accepted

This should do what you want:

add_action('generate_rewrite_rules', 'wpse128082_add_rewrite_rule');
function wpse128082_add_rewrite_rule($wp_rewrite) {
    $new_rules = array(
        '^charts/(.*)' => 'index.php?pagename=charts&something='.$wp_rewrite->preg_index(1),
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_filter('query_vars', 'wpse128082_add_query_var');
function wpse128082_add_query_var($query_vars) {
    $query_vars[] = 'something';
    return $query_vars;
}

On your Charts page, you can access the new query var with get_query_var('something').

share|improve this answer
    
I suggest adding rewrite rules via the API: add_rewrite_rule. –  Milo Dec 31 '13 at 19:06
    
@t f, thanks. however, the add_rewrite_rule functions worked great for me. –  Shazzad Jan 5 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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