It’s common for WordPress sites to create post URLs with a base slug — like /blog/%postname%
, where /blog/ is the base of the slug (i.e. the URL). A lot of WP sites are websites first and blogs second. Unfortunately, when you’re using a base slug and create a custom post type (CPT) the CPT is going to prepend the base slug in front of its own slug too.
This article explains how to prevent custom post types from prepending the base slug in WordPress URLs. Learn how to register custom post types and remove the double front slug, so your website URLs can be structured how you’d like them to be — in a way that makes sense.
WordPress Permalink CPT Structure
Out of the box, WordPress does URLs the wrong way. I’m sure there’s a well thought reason for this, but regardless it’s not what you want.
For blog posts, a URL slug of /blog/%postname%
for single-page post content is great. That structure makes perfect sense since most blog posts aren’t going to be first-tier pieces of content on your website. First-tier content is typically reserved for level-1 pages like /about, /services, /contact and so forth – those are the most important pages on your site and branch directly off the domain name.
Blog posts and custom post types are not necessarily first-tier content. But, they’re also not the same type of second-tier content. Therefore, they deserve their own unique URL slug structures instead of being a strange mashup of conjoined taxonomies.
WordPress CPT URL Without Default Post’s Front Slug
For custom post types, a URL slug of /blog/<post-type-term>/<post-type-post-title>
doesn’t make much sense at all. You don’t want to see /blog/ prepended to the front of the slug that you already registered to be the front of the CPT.
Instead, you can remove the blog post’s front slug and create a custom post type with only its own front slug. There’s no need to double up. Let’s take a look at some example code.
Solution
The solution for how to remove the prepended default post’s slug from custom post type slugs is to add 'with_front' => false
into the rewrite
array values when registering your custom post type. Yes, it’s really that simple.
Example Code
Take a look at the example code below that’s registering a custom post type named “Work”.
function create_post_type_work() { register_post_type('work', array( 'labels' => array( 'name' => __('Work', 'work'), 'singular_name' => __('Work', 'work'), 'add_new' => __('Add New', 'work'), 'add_new_item' => __('Add New Work', 'work'), 'edit' => __('Edit', 'work'), 'edit_item' => __('Edit Work', 'work'), 'new_item' => __('New Work', 'work'), 'view' => __('View Work', 'work'), 'view_item' => __('View Work', 'work'), 'search_items' => __('Search Work', 'work'), 'not_found' => __('No Work found', 'work'), 'not_found_in_trash' => __('No Work found in Trash', 'work') ), 'public' => true, 'hierarchical' => false, 'has_archive' => false, 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', ), 'can_export' => true, 'exclude_from_search' => true, 'rewrite' => array( 'slug' => 'work', // Add this to prevent the default post's slug 'with_front' => false ), )); } add_action('init', 'create_post_type_work');
Explanation
The code above is an example of registering a custom post type named “Work”.
It’s defining all the necessary labels and properties that tell the CPT how to behave and display in the WP dashboard. And right at the bottom you can see the with_front
property set to false.
... 'rewrite' => array( 'slug' => 'work', // Add this to prevent the default post's slug 'with_front' => false ), ...
The slug property is the slug-front for the custom post type.
with_front tells the custom post type not to use the default post’s custom post type.
Last Step: Flush the Permalinks
Whenever you’re changing URL structure for a WordPress site the last step is always flushing the permalinks. Navigate to the WP dashboard, Settings, Permalinks and click the Save Changes button to finalize your edits. And then it’s safe to test your work.
0 Comments