I am trying to: remove the custom post type slug from the url, and add a .html extension to the end of the url
I visited this URL: hidden link
I expected to see: content of custom post type
Instead, I got: Nothing Found error. Page can't be found.
I have a child theme of blank, generatepress_child.
I have added the following functions - one to remove the custom post slug, and another to add the .html extension to the URL.
Both work independently. Either I can add the .html, or I can remove the custom post slug.
BUt when I put both functions in the functions.php, the URL shows correctly (without slug, and with .html extension), but does not show any content.
TO ADD .HTML extension
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;
}
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' );
}
add_filter( 'redirect_canonical', '__return_false' );
TO REMOVE custom post SLUG
/**
* Remove the slug from published post permalinks. Only affect our custom post type, though.
*/
function gp_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'course' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );
/**
* Have WordPress match postname to any of our public post types (post, page, race)
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* By default, core only accounts for posts and pages where the slug is /post-name/
*/
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'page', 'course' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );
Kindly suggest how to combine these 2 functions together so they can achieve both at the same time.
Thanks,