Search code examples
phpwordpresspluginscustom-post-typeslug

How to change slug in wordpress?


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.


Solution

  • 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