Recently while working on the client project, we wanted to add a string ‘/blog/’ to the WordPress post URLs. I don’t see any specific reason why we should add ‘/blog/’ to a single post URL. On this blog, I am keeping simple URLs for my post. However, the client wanted this URL format in their project.
Assuming, you have set permalink settings to ‘post name’, WordPress post URL will have the following format:
After adding ‘/blog/’ to the post URLs it will look like:
Let’s take a look at how one can do this in their WordPress website.
Add ‘/blog/’ to the WordPress Post URLs
Login to your WordPress dashboard and go to the Settings->Permalink. On this page, choose the option ‘Custom Structure’ and enter /blog/%postname%/ in the given field. Refer to the screenshot below.
Don’t forget to hit the ‘Save Changes’ button. Otherwise, it will not take effect. Now check your post and category URLs, it should contain the ‘/blog/’ in the respective URLs.
Your job is not over yet. The above steps will also affect your custom post type and custom taxonomy URLs.
Let’s say you have a custom post type ‘product’ and custom taxonomy ‘product_cat’ in your WordPress website. After performing the above steps post type and taxonomy URLs will also contain ‘/blog/’ as follows.
If you are okay with this format then no worries. But if you don’t want ‘/blog/’ in your custom post type and custom taxonomy URLs then you have to add one more parameter to the rewrite rule.
Both these methods have a
rewrite parameter. To this parameter, you have to pass the key
'with_front' => false which keeps your URLs unaffected. Your code will be something like below.
// custom post type 'product' register_post_type( 'product', array( ... ... 'rewrite' => array('slug' => 'product', 'with_front' => false), ) ); // custom taxonomy 'product_cat' register_taxonomy( 'product_cat', 'product', array( ... ... 'rewrite' => array('slug' => 'product_cat', 'with_front' => false), ) );
'with_front' => false to both register_post_type() and register_taxonomy(), you must update the permalinks on Settings->Permalink page. After this you can see ‘/blog/’ is added only to your default post and categories. Your custom post types and custom taxonomy URLs remain intact.
I hope you got to know how to add ‘/blog/’ to the WordPress post URLs. If needed you use any other string and follow the steps defined in the article. I would like to hear your thoughts and suggestions in the comment section below.
- How to Integrate Mailchimp with WooCommerce
- Login with Phone Number in WordPress
- Load Dynamic Content on Bootstrap Modal in WordPress
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.