How can I add pages under the URL structure of a Custom Post Type?

For example…

If I had a Custom Post Type called “Knowledge Base” … Located at: domain.com/knowledge-base/

… and I wanted to add a page called “Archives” … Located at: domain.com/knowledge-base/archives/

… and I wanted to add sub pages to the archives like … daily, weekly, monthly, yearly, etc. … Located at: domain.com/knowledge-base/archives/daily/ (for example).

The only way I can think of doing it is I have to add a page with a slug of “knowledge-base”, then add respective sub pages under that.

However, when doing that… The rewrite rules for the page seem to overwrite my post type archive when viewing domain.com/knowledge-base/.

OR

The rewrite rules for the custom post type overwrite the page rewrite rules.

Either way, I’m not able to have a fully functioning custom post type with sub pages appended to it’s structure.

How can you add “pages” to your custom post type?

1 Answer
1

I really should just try to think things out thoroughly before instantly asking questions.

I found a very easy solution. It’s not exactly adding “pages” but it does what I need.

When registering the custom post type, there are two things which need to be specified.

By default, the parameter hierarchical is set to false. Solution: set hierarchical => true;

In order for the hierarchical functionality to work, you need to add page-attributes to the supports parameter.

Now, just create a new post in your custom post type called “archives” and select no parent.

Then create another new post, “daily” (for example), and select “archives” as the post parent.

Easy enough.

However, if you have a “page” with a slug of “archives”, it seems that your knowledge base “archives” will have a slug of “archives-2” instead of just “archives”.

If you encounter that problem, then update to WordPress 4.1. It allows duplicate slugs across multiple post types.

Leave a Reply

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