theme development - Remove CPT slug from URL WordPress

I have created custom post type with the slug (example: loan-officer). I want to remove CPT slug from the URL. I have us

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
Add a comment  | 

1 Answer 1

Reset to default 0

Based 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742319063a4421435.html

相关推荐

  • theme development - Remove CPT slug from URL WordPress

    I have created custom post type with the slug (example: loan-officer). I want to remove CPT slug from the URL. I have us

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信