admin管理员组文章数量:1332361
I have created custom post type with the slug (example: loan-officer). I want to remove CPT slug from the URL. I have used below code, I have seen so many threads like this, still, nothing is working.
function custom_post() {
register_post_type(
'loan_officers', array(
'labels' => array('name' => __( 'Loan Officers' ), 'singular_name' => __( 'Loan Officer' ) ),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-calendar-alt',
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
'publicly_queryable' => true, // you should be able to query it
'show_ui' => true, // you should be able to edit it in wp-admin
'show_in_menu' => true,
'show_in_admin_bar' => true,
'exclude_from_search' => false, // you should exclude it from search results
'show_in_nav_menus' => true, // you shouldn't be able to add it to menus
'has_archive' => false, // it shouldn't have archive page
'rewrite' => array(
'slug' => 'loan-officer',
'with_front' => false,
)
)
);
}
add_action( 'init', 'custom_post');
function gp_remove_cpt_slug( $post_link, $post ) {
if ( 'loan_officers' === $post->post_type && 'publish' === $post->post_status ) {
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 );
function gp_add_cpt_post_names_to_main_query( $query ) {
if ( ! $query->is_main_query() ) {
return;
}
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
if ( empty( $query->query['name'] ) ) {
return;
}
$query->set( 'post_type', array( 'post', 'page', 'loan_officers' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
Still, the slug is coming in the URL when I tried to view from admin and without slug also it's working. Can anyone suggest how to redirect from
domain/loan-officer/testing to domain/testing
Thanks.
I have created custom post type with the slug (example: loan-officer). I want to remove CPT slug from the URL. I have used below code, I have seen so many threads like this, still, nothing is working.
function custom_post() {
register_post_type(
'loan_officers', array(
'labels' => array('name' => __( 'Loan Officers' ), 'singular_name' => __( 'Loan Officer' ) ),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-calendar-alt',
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
'publicly_queryable' => true, // you should be able to query it
'show_ui' => true, // you should be able to edit it in wp-admin
'show_in_menu' => true,
'show_in_admin_bar' => true,
'exclude_from_search' => false, // you should exclude it from search results
'show_in_nav_menus' => true, // you shouldn't be able to add it to menus
'has_archive' => false, // it shouldn't have archive page
'rewrite' => array(
'slug' => 'loan-officer',
'with_front' => false,
)
)
);
}
add_action( 'init', 'custom_post');
function gp_remove_cpt_slug( $post_link, $post ) {
if ( 'loan_officers' === $post->post_type && 'publish' === $post->post_status ) {
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 );
function gp_add_cpt_post_names_to_main_query( $query ) {
if ( ! $query->is_main_query() ) {
return;
}
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
if ( empty( $query->query['name'] ) ) {
return;
}
$query->set( 'post_type', array( 'post', 'page', 'loan_officers' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
Still, the slug is coming in the URL when I tried to view from admin and without slug also it's working. Can anyone suggest how to redirect from
domain/loan-officer/testing to domain/testing
Thanks.
Share Improve this question asked Jun 24, 2020 at 18:15 Sudhakar SJSudhakar SJ 111 bronze badge 2- 1 Does this answer your question? Remove slug from custom post type post URLs – Luke Commented Jun 24, 2020 at 19:09
- 1 No, both are not the same thread, removing slug from URL it's working. But at the same time, two URL working. One with slug and without slug. Both URL is linking to the same post. – Sudhakar SJ Commented Jun 25, 2020 at 2:24
1 Answer
Reset to default 0Based on the answers provided in the question that I linked to, you would probably be best served to use a plugin such as Permalink Manager Lite. It looks after the redirecting of old URLs for you and also manages the issue of duplication.
The code only option would be this answer but that depends on how much you want to code yourself.
本文标签: theme developmentRemove CPT slug from URL WordPress
版权声明:本文标题:theme development - Remove CPT slug from URL WordPress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742319063a2452411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论