Recently while working on the client’s project, we wanted to add a string ‘/blog/’ to the WordPress post URLs. Usually we set permalink structure to Post name which generate the following URL format for your posts.
Where ‘hello-world’ is the slug created by WordPress based on the name of post.
After adding ‘/blog/’ to the post URLs the URL will look like this:
Here the string ‘blog’ in the URL clearly indicates this is the post URL. Having said that, let’s take a look at how one can achieve this on their WordPress website.
Add ‘/blog/’ to the WordPress Post URLs
Login to your WordPress dashboard and go to Settings->Permalink. On this page, choose the option Custom Structure and enter /blog/%postname%/ in the given field. Refer to the screenshot below.
Hit the Save Changes button, otherwise it will not take effect. Now check your post and category URLs, it should contain the ‘/blog/’ in their URLs.
But wait our 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’ on your website. After performing the above steps this post type and taxonomy URLs will also contain ‘/blog/’ as follows.
Most probably you don’t want this format. To remove ‘/blog/’ from your custom post type and taxonomy URLs you have to add one more parameter to the rewrite rule.
Both these methods have a
rewrite key. To this parameter, you require to pass the key
'with_front' => false to keep your URLs intact. 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), ) );
Once you added
'with_front' => false as shown above, you must update the permalinks on Settings->Permalink page. After this you will see ‘/blog/’ is added only to your default posts and categories. Your custom post types and custom taxonomy URLs remain intact.
In this tutorial, we added the string ‘/blog/’ to the WordPress post URLs. If needed you can use any other string and follow the same steps defined in the article.
- 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.