Amp Remove Custom Post Type Slug

I’m trying to figure out this problem: first of all, I added a custom post type to my theme and I removed the slug of cpt (so the url was “https://siteurl/recipe/post-name” and now it is “https://siteurl/post-name“) with this function:

function remove_cpt_slug( $post_link, $post, $leavename ) {

if ( ‘recipe’ != $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’, ‘remove_cpt_slug’, 10, 3 );

It works good but now I’m trying to do the same thing to amp version:

if (is_amp_endpoint() && (‘recipe’ === $post->post_type )) {
$post_link = str_replace( ‘/’ . $post->post_type . ‘/’, ‘/’, $post_link.’/amp’ );

return $post_link;

but the amp page is actually located to “https://siteurl/recipe/post-name/amp” and not as I’d want “https://siteurl/post-name/amp

Is my approach wrong? Can anyone help me to solve it?

Thanks a lot.

Read more here:: Amp Remove Custom Post Type Slug

Leave a Reply

Your email address will not be published. Required fields are marked *