Home > other >  How to change custom post type permalink?
How to change custom post type permalink?

Time:04-24

In my wordpress website :

I need to modify permalink for the custom post type Rivista Quadrimestrale.

Now the permalink is :

https://www.sicilianmarketing.it/diritticomparati/rivista-trimestrale/%postname%

I need it to be

https://www.sicilianmarketing.it/diritticomparati/%postname%

Please help in this case.

CodePudding user response:

You can do it in apache or nginx if needed. You need to give the information to the browser by giving a 301 status code and the new link if called on the old.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301

CodePudding user response:

In your functions.php try adding

function cpt_remove_slug( $post_link, $post, $leavename ) {
    if ( 'rivista-trimestrale' != $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', 'cpt_remove_slug', 10, 3 );



// Removes  the slug 

function cpt_parse_request( $query ) {
    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {        return;    }
    if ( ! empty( $query->query['name'] ) ) {        $query->set( 'post_type', array( 'post', 'rivista-trimestrale', 'page' ) );    }
}
add_action( 'pre_get_posts', 'cpt_parse_request' );
  • Related