Here’s a complete working example that adds a post type, with extra rule to capture an additional parameter:
function wpd_post_type_and_rule() {
register_post_type( 'mycpt',
array(
'labels' => array(
'name' => __( 'mycpt' ),
),
'public' => true,
'rewrite' => array( 'slug' => 'mycpt' ),
)
);
add_rewrite_tag( '%mycpt_var%', '([^/]*)' );
add_rewrite_rule(
'^mycpt/([^/]*)/([^/]*)/?$',
'index.php?mycpt=$matches[1]&mycpt_var=$matches[2]',
'top'
);
}
add_action( 'init', 'wpd_post_type_and_rule' );
After adding this and flushing rewrite rules, you’ll have both
www.site.com/mycpt/the-name-of-my-post/
and
www.site.com/mycpt/the-name-of-my-post/var-value-here/
You can get the value of mycpt_var
in the template with:
echo get_query_var( 'mycpt_var' );