admin管理员组

文章数量:1332713

I want to use custom template redirects for urls such as and

I'm using add_rewrite_rule function for this

add_action('init', function() { 
  add_rewrite_rule( "^foo/([^/]*)/?", 'index.php?foo=1&bar=$matches[1]', 'top' ); 
})

Then I set the variables

add_filter( 'query_vars', function( $vars ){
    $vars[] = 'foo'; 
    $vars[] = 'bar';
    return $vars;
}

add_filter('template_include', function($original_template) {
    if(get_query_var('foo')) {
        return '/my-template.php'
    } else {
        return $original_template;
    }
}

It works fine for links like and in my template I can use bar variable.

get_query_var('bar');

But / returns the 404 error.

Can I make this link with template redirect too?

本文标签: Template includes