Skip to content

Instantly share code, notes, and snippets.

@NatalieMac
Last active August 29, 2015 14:11
Show Gist options
  • Save NatalieMac/cb44b90a4b749f2dd936 to your computer and use it in GitHub Desktop.
Save NatalieMac/cb44b90a4b749f2dd936 to your computer and use it in GitHub Desktop.
Set up multiple views of a single template that appear as subpages in the URL (WordPress)
function prefix_cpt_rewrite() {
add_rewrite_tag( '%view%', '([^&]+)' );
}
add_action( 'init', 'prefix_cpt_rewrite' );
function prefix_cpt_rewrite_rules() {
$slug = 'cpt-slug';
$param_name = 'view';
$param_val = 'map';
add_rewrite_rule(
$slug . '/([^/]*)/' . $param_val . '/?',
'index.php?post_type=' . $slug . '&name=$matches[1]&' . $param_name . '=' . $param_val,
'top' );
add_rewrite_rule(
$slug . '/([^/]*)/([^/]*)/' . $param_val . '/?',
'index.php?post_type=' . $slug . '&name=$matches[2]&' . $param_name . '=' . $param_val,
'top' );
}
add_action( 'init', 'prefix_cpt_rewrite_rules' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment