How To Change the Slugs for Default Posts and Custom Post Types
In WordPress, the default post slugs are automatically generated from post titles, resulting in URLs like example.com/hello-world
. However, to achieve better branding and SEO, you might need more control over your website’s permalinks. The good news is that WordPress offers a straightforward way to customize permalinks without relying on plugins.
In this tutorial, we’ll walk you through the step-by-step process of changing default post slugs and those of custom post types while adhering to WordPress coding standards. Whether you’re a beginner or an experienced developer, these instructions will empower you to create user-friendly, descriptive, and SEO-friendly URLs that align perfectly with your website’s objectives. Let’s dive in and elevate your WordPress website’s URL structure for improved branding and online visibility.
Permalink Custom Structure for Default Posts
- Go to the WordPress dashboard, navigate to “Settings” -> “Permalinks.”
- In the “Common Settings” section, choose the “Custom Structure” option.
- Enter the following in the input field to set a custom permalink structure:
/blog/%postname%/
Code language: PHP (php)
By adding /blog/
followed by %postname%
, you are configuring WordPress to use “blog
” as a prefix in the URL for default posts.
Configure the Custom Post Types
When you create custom post types in WordPress using plugins like Advanced Custom Fields (ACF) or ACF Extended, they come with default settings, including rewrite rules that determine the URL structure for these posts.
For example, a custom post type named “Books.” By default, the URLs for books would look like example.com/blog/book/sample-book-title
if you don’t update the rewrite rules. This structure includes the /blog/
prefix inherited from regular blog posts and might not be desirable for the “Books” custom post type.
Updating the rewrite rules for custom post types is crucial for maintaining a clean and meaningful URL structure. Without customization, the URLs might become unnecessarily long and less user-friendly.
Depending on the plugin used to set up custom post types, follow the appropriate steps below:
Advanced Custom Fields (PRO)
- Go to ACF > Post Types in the WordPress dashboard.
- Edit the Custom Post Type for which you want to change the slug.
- In the Advanced Settings > URLs, disable the Front URL Prefix option.
By disabling the front URL prefix, you ensure that custom post types don’t have any additional prefixes before the slug.
ACF Extended (PRO)
- Go to Tools > Post Types in the WordPress dashboard.
- Edit the Custom Post Type you wish to modify.
- In the Single tab, set Rewrite to Yes and Rewrite Arguments to Yes.
- In the Rewrite Arguments, ensure With front is set to No.
By making these adjustments, you remove the front part of the slug, making it cleaner and more SEO-friendly.
Custom Post Type Without Plugins
If you’ve registered a custom post type directly in your theme’s functions.php
file or within a custom plugin without using any additional plugins, use the following code snippet:
// Add custom post type "books"
function custom_theme_create_books_post_type() {
register_post_type(
'books',
array(
'labels' => array(
'name' => __( 'Books' ),
'singular_name' => __( 'Book' ),
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'book',
'with_front' => false,
),
)
);
}
add_action( 'init', 'custom_theme_create_books_post_type' );
Code language: PHP (php)
In the above example, we have used the 'rewrite'
parameter in the register_post_type()
function to customize the slug for the “books” custom post type. In this case, we have set the slug to be 'book'
, so the permalink for a “books” post will now look like example.com/book/sample-book-title
.
Additionally, including 'with_front' => false
ensures that the custom post type won’t have any additional prefixes before the slug, resulting in cleaner and more SEO-friendly URLs.
Remember to flush the rewrite rules after making changes to the permalink structure. You can do this by visiting the “Settings” -> “Permalinks” page in the WordPress dashboard and simply clicking the “Save Changes” button.
With this modification, the custom post type “books
” will have its slugs customized according to the specified structure.
That’s it! You’ve successfully learned how to customize default post slugs and custom post type slugs in WordPress. With the power to craft user-friendly, descriptive, and SEO-friendly URLs, you can enhance your website’s branding, search engine rankings, and overall user experience.
Always be cautious when modifying permalink structures, and thoroughly test your changes to ensure a seamless browsing experience for your visitors.
Leave your feedback and help us improve 🐶
We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.
Your thoughts matter, leave a reply 💬