简体   繁体   English

如何更改自定义帖子类型永久链接?

[英]How to change custom post type permalink?

In my wordpress website:在我的 wordpress 网站中:

I need to modify permalink for the custom post type Rivista Quadrimestrale.我需要修改自定义帖子类型Rivista Quadrimestrale 的固定链接。

Now the permalink is:现在永久链接是:

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

I need it to be我需要它是

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

Please help in this case.请在这种情况下提供帮助。

You can do it in apache or nginx if needed.如果需要,您可以拨打 apache 或 nginx。 You need to give the information to the browser by giving a 301 status code and the new link if called on the old.您需要通过提供 301 状态代码和新链接(如果在旧链接上调用)来向浏览器提供信息。

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

In your functions.php try adding在你的functions.php尝试添加

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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM