This seem to work:
Create the rewrite rules like post-type/post-name.html
. You can use arrays to create the rules for just some set of post types instead of doing it for all of them.
add_action( 'rewrite_rules_array', 'rewrite_rules' );
function rewrite_rules( $rules ) {
$new_rules = array();
foreach ( get_post_types() as $t )
$new_rules[ $t . '/([^/]+)\.html$' ] = 'index.php?post_type=' . $t . '&name=$matches[1]';
return $new_rules + $rules;
}
Format the new permalink structure for these post types.
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
global $post;
$type = get_post_type( $post->ID );
return home_url( $type . '/' . $post->post_name . '.html' );
}
And then stop redirecting the canonical URLs to remove the trailing slash. This might need some more work, as you'll probably want to keep the redirection for most cases.
add_filter( 'redirect_canonical', '__return_false' );
As others said around here, after doing the above you'll need to flush the rules, and that's possible by visiting the options-permalink.php
admin page in Dashboard -> Settings -> Permalinks
.
function post-type_permalink
will throw an error. If you don't receive this error, then you've got something painfully wrong with your debug settings. Also please rework your question with the correct intends on the lines. Thanks./$postname.html
for the posts, how can I use the same for the custom post types.-
doesn't work in function names...