How to change slug in wordpress?

走远了吗. 提交于 2021-01-28 08:06:15

问题


I have this URL for my Resort page page-resorts.php:

http://localhost/testwordpress/resorts/

After clicking the link to a post under Resort custom page I will have this URL for my custom page template (CPT) single-resort.php:

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

As you can see the resorts was changed to resort because I can't use resorts slug for the slug post.

How can I achive this kind of URL:

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

where the resorts word is used and not resort word only?

I've heard about custom slug and search for it, but I still can't understand it.


回答1:


You can create your custom post type this way.

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'), this used to add the slur on URL. this way you can acheive your URL.

Or you can override your current slug in functions.php

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;
}

For more, please visit.

register post type

Change Custom Post Type slug



来源:https://stackoverflow.com/questions/52290853/how-to-change-slug-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!