How to Add ‘/blog/’ to the WordPress Post URLs

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.

If you are someone who also looking for same URL structure then keep reading.

When you set permalink settings to ‘post name’, WordPress post URL will have the following format:

SITE_URL/hello-world

After adding ‘/blog/’ to the post URLs it will look like:

SITE_URL/blog/hello-world

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.

custom-structure

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 their URLs.

It seems like our job is over but it’s not 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 custom post type and taxonomy URLs will also contain ‘/blog/’ as follows.

SITE_URL/blog/product/test-product
SITE_URL/blog/product_cat/table

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.

While creating post type, we use the register_post_type() method. In the case of custom taxonomy, the method register_taxonomy() is being used.

Both these methods have a rewrite key. To this parameter, you have to pass the key 'with_front' => false to 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),
    )
);

After adding '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 can use any other string and follow the steps defined in the article.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *