Separating Blog & Custom Post Type Permalink

If you have custom post type registered on your WordPress based site, it automatically separates the blog section/Posts from your CPT posts. Regardless of the settings for your permalink structure, CPT would follow its default permalink structure and regular permalink structure would have no effect on CPT posts. Let me give you an example. Let’s say your general permalink structure has been set to something like this:

www.yourdomain.com/2013/blog-post-title

or

www.yourdomain.com/blog-post-title

Also assume that you also have a custom post type called “Books” and your post register functions has “has_archive” parameter set to true/books and the rewrite parameter has set the slug to “books”. In such case the permalink structure for your custom post type should be something like this:

www.yourdomain.com/books/

This url would act as the archive page for books post type. And the following permalink for any single post that belongs to your books post type.

www.yourdomain.com/books/cpt-post-title

Now, lets take serious look at the top two URL and the bottom two for the CPT. Did you see the fundamental differences between them?

Top two URL was for the “blog” section our site. However, the permalink structure doesn’t quite say so. Wouldn’t it be better if we had the following permalink for the Blog/ Posts section?

Blog Home Page

www.yourdomain.com/blog/

For single blog post

www.yourdomain.com/blog/blog-post-title

For Blog Categories Archive

www.yourdomain.com/blog/category/

For Post Tags Archive

www.yourdomain.com/blog/tags/

At least we would had a “blog” section entirely separated from any other URLs.

How do we do that?

Step 1: The very first thing you need to do is to create a page with the “Home” title and publish it. If you want to customize the look and feel, you can use any custom template you want.

Step 2: Create another page with “Blog” title. Don’t type anything else on that page, from the template section select “default template” and simply publish it. We are doing this to make sure that “index.php” file of your theme would be the home page for blog section and regular loop would show the latest blog posts.

Step 3: Now lets go to “Settings” menu and select “Reading” page. From the “Front page displays” section select the “A static page (select below)” radio button. For “Front Page” select the “Home” page that you just created and for “Posts Page” select the “Blog” page.

Step-3: Now, lets head toward the “Settings” menu and click on “Permalink”. From the “Common Settings” section, select “Custom Structure” radio button and type /blog/%postname%/ on the input field. This is to make sure that the single blog post of your site would be something like this:

www.yourdomain.com/blog/blog-post-title

However, you can change the “postname” structure tag with something else. Check out the list of Structure Tags that can be used here.

Now, go to the “Optional” section and for “Category base” type blog/category and for “Tag base” type blog/tag. Now simply click on “Save Changes” button. That’s it. You are done.

Important note: Above settings would force your Custom Post Type (Books) to have “blog” slug ahead of the URL and it would look like:

https://www.yourdomain.com/blog/books/post-name/

To get rid of the “blog” slug from the URL, make sure you have the “with_front” parameter from “rewrite” argument set to “false” on your custom post type register function. Here is the sample:

'rewrite' => array('slug' => 'books', 'with_front' => false),

This would give you cleaner permalink structure for both the blog section and for CPTs.

Once you are done with steps mentioned above, simply go to the front end and refresh the page. Now, you should have totally separated URL structure for both the CPT and Blog. Hope enjoyed.

Today In History

  •  
  •  

Comment

2 Comments

  1. NeyPublished: 3 years ago

    Very cool your tips too 🙂

    Thank you very much.

    • IftekharPublished: 3 years ago

      Hello Ney, you are more than welcome 🙂 I am just happy to know that you found this post to be useful. Enjoy.

Leave a Reply to Ney

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.