plugins - Remove POST_TYPE from custom post type permalink

I have a post type named services with the following permalink DOMAINservicesinvesting (Single Page Permalink). I wan

I have a post type named services with the following permalink DOMAIN/services/investing/ (Single Page Permalink).

I want the permalink to be changed like DOMAIN/investing/. I don't want the POST_TYPE in my permalink.

I updated the permalink settings in WP Dashboad. But it only changed to blog posts, not to the custom post types.

How can I customize the permalink of custom post types?

The POST_TYPE is created using register_post_type as below.

function cp_services() {
  register_post_type('Services', array(
    'labels' => array(
        'name' => 'Services',
        'singular_name' => 'service',
        'add_new_item' => 'Add New Service',
        'edit_item' => 'Edit Service',
    ),
    'description' => 'Services',
    'public' => true,
    'has_archive' => true,
    'menu_position' => 20,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'supports' => array('title','editor','thumbnail')
  ));
}

add_action('init', 'cp_services');

I have a post type named services with the following permalink DOMAIN/services/investing/ (Single Page Permalink).

I want the permalink to be changed like DOMAIN/investing/. I don't want the POST_TYPE in my permalink.

I updated the permalink settings in WP Dashboad. But it only changed to blog posts, not to the custom post types.

How can I customize the permalink of custom post types?

The POST_TYPE is created using register_post_type as below.

function cp_services() {
  register_post_type('Services', array(
    'labels' => array(
        'name' => 'Services',
        'singular_name' => 'service',
        'add_new_item' => 'Add New Service',
        'edit_item' => 'Edit Service',
    ),
    'description' => 'Services',
    'public' => true,
    'has_archive' => true,
    'menu_position' => 20,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'supports' => array('title','editor','thumbnail')
  ));
}

add_action('init', 'cp_services');
Share Improve this question edited Jul 2, 2019 at 15:10 Ramesh asked Jul 2, 2019 at 15:00 RameshRamesh 1451 silver badge9 bronze badges 4
  • How are you creating the custom post type? By plugin or register_post_type? – ChristopherJones Commented Jul 2, 2019 at 15:06
  • @ChristopherJones register_post_type – Ramesh Commented Jul 2, 2019 at 15:08
  • 1 See wordpress.stackexchange/questions/203951/… - it's complicated, and you run the risk of conflicting with Pages or Posts if you create any of them with the same slugs. – WebElaine Commented Jul 2, 2019 at 22:36
  • @WebElaine This worked in my case – Ramesh Commented Jul 3, 2019 at 7:52
Add a comment  | 

1 Answer 1

Reset to default 1

As @WebElaine commented on this question I tried the following code which worked for me.

First, remove the slug from the permalink

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

  if ( 'services' != $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', 'na_remove_slug', 10, 3 );

Just removing the slug isn't enough. Right now, you'll get a 404 page because WordPress only expects posts and pages to behave this way. You'll also need to add the following:

function na_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', 'services', 'page' ) );
  }
}

add_action( 'pre_get_posts', 'na_parse_request' );

Then refresh the permalink.

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

相关推荐

  • plugins - Remove POST_TYPE from custom post type permalink

    I have a post type named services with the following permalink DOMAINservicesinvesting (Single Page Permalink). I wan

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信