4

I would like to turn /{custom-post-type}/{post-name} to /{post-name}, is this possible?

If this is not possible, can I change the labels/terminology in Pages or Posts so that they could be called "Clients" or "Projects" etc?

10

You need to use option 'rewrite' in your custom post type registration.

E.g. 'rewrite' => array('slug' => 'products'),

From the codex

When you namespace a URL and still want to use a "clean" URL structure, you need to add the "rewrite" element to the array. For example, assuming the "ACME Widgets" example from above:

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'acme_product',
      array(
          'labels' => array(
              'name' => __( 'Products' ),
              'singular_name' => __( 'Product' )
          ),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'products')
      )
  );
}

The above will result in a URL like http:/example.com/products/%product_name% (see description of %product_name% above.) Note that we used a plural form here which is a format that some people prefer because it implies a more logical URL for a page that embeds a list of products, i.e. http:/example.com/products/.

Also note using a generic name like "products" here can potentially conflict with other plugins or themes that use the same name but most people would dislike longer and more obscure URLs like http:/example.com/acme_products/foobrozinator and resolving the URL conflict between two plugins is easier simply because the URL structure is not stored persistently in each post record in the database in the same way custom post type names are stored.

2
  • I think you misunderstand, I want the posts to have clean URLs but I do not want to have a post-type slug (the method you have quoted from the codex). – Tom Jan 11 '12 at 11:20
  • Thank you - worked for me! – michaelmcgurk Jul 31 '19 at 9:14
8
'rewrite' => array('slug' => '/', 'with_front' => false)

This will get the url correctly formed for you, but it will 404 unless you handle it in your theme.

1
  • True about 404, but as @Subhankar Adhikary mentions, resaving permalinks fixes it – Fanky May 5 '20 at 11:54
4

I just changed a slug for a custom post type (in this case, I changed it from "best-practices" to "bestpractices"). I had copied some code that had a blank slug, like so:

'rewrite' => array('slug' => ''),

Since it was originally left blank, Wordpress was just using the name of the custom post type ("best-practices") as the slug. I changed my line of code to

'rewrite' => array('slug' => 'bestpractices'),

I just wanted to note here that you'll need to reset your permalinks to get the change to take effect, if you're changing slugs on pre-existing custom post types.

1
  • 2
    THANK YOU! I just spent an hour trying to figure out why my permalinks weren't updating. I didn't realize you'd need to go into the admin and "Save" your permalink structure again. – Nathan J.B. Aug 27 '14 at 6:17
1

yes, we can change the slug. Please follow the steps:

Step 1: Change the slug name from the option "rewrite". (located at Theme Functions (functions.php))

'rewrite' => array('slug' => 'products')

Step 2: Reset the permanent link. Otherwise, it will give 404 error. 1> goto wp dashboard. 2> Setting --> Permalinks

Under the common setting section--- 1st change it to plain and save it. After that, again change the setting as your preferred format.

1
  • It's enough to just click the save button in the permalinks section, I think no changes need to be done. – Fanky May 5 '20 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.