如何更改 wordpress 中的 slug?

How to change slug in wordpress?

我的 度假村 页面有这个 URL page-resorts.php:

http://localhost/testwordpress/resorts/

点击 link 到 Resort 自定义页面下的 post 后,我将为我的自定义页面模板(CPT ) single-resort.php:

http://localhost/testwordpress/resort/kurumba-maldives/

如您所见,resorts 已更改为 resort,因为我无法将 resorts slug 用作 slug post.

我怎样才能实现这种URL:

http://localhost/testwordpress/resorts/kurumba-maldives/

哪里使用了 resorts 词而不是 resort 词?

我听说过custom slug 并搜索过,但我仍然无法理解。

您可以通过这种方式创建自定义 post 类型。

function create_posttype() {
  register_post_type( 'resort',
    array(
      'labels' => array(
        'name' => __( 'Resorts' ),
        'singular_name' => __( 'Resort' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'resorts'),
    )
  );
}
add_action( 'init', 'create_posttype' );

'rewrite' => array('slug' => 'resorts'), 这用于在 URL 上添加连线.这样你就可以实现你的 URL.

或者您可以在 functions.php

中覆盖当前的 slug
add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
function wpse247328_register_post_type_args( $args, $post_type ) {

    if ( 'resort' === $post_type ) {
        $args['rewrite']['slug'] = 'resorts';
    }

    return $args;
}

更多请访问

register post type

Change Custom Post Type slug