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', 'remove_redirect_canonical');
function remove_redirect_canonical($redirect_url) {
return false;
}
As others said around here, after doing the above you'll need to flush the rules, and that's possible by changing the permalink structure in Dashboard -> Settings -> Permalinks to default, and then switch it back to a rewrite model.
function post-type_permalinkwill 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. – kaiser Jul 25 '12 at 15:11/$postname.htmlfor the posts, how can I use the same for the custom post types. – user983248 Jul 25 '12 at 15:21-doesn't work in function names... – kaiser Jul 25 '12 at 15:23