In navigation logic, the page Y should be a descendant of the category X
In WordPress, posts can have categories, but pages can’t. Pages have parent and child pages. If you want categories, you’ll need to use posts.
There may be a plugin that allows pages to use categories, but I haven’t searched for one.
register_taxonomy_for_object_type( 'category', 'page' );
@kjodle, how would you approach a case in which you need to have a page with static content on it and that page needs to appear in the URL as a category’s descendant?
@bcworkz, isn’t that function just adding a category as a taxonomy of page? I need the opposite to happen. Page should be the category’s descendant. But maybe I am not evaluating your suggestion correctly…
Thank you guys,
N
Like kjodle indicated, by default you cannot even assign any category term to a page, there is no established relationship. The code I posted establishes the relationship, it is required to do anything with categories and pages together.
It’s not possible for any one object to be a descendant of a different object type, descendants must be of the same type. Just like you cannot have a horse be a child of a dog. Only dogs can be children of dogs 🙂 You can assign any term to any page after my code. The term or page may be a descendant of any other like type, so some kind of relationship is possible, but not like you are thinking.
You may have issues with the permastruct that you desire. You can add a rewrite rule so that they work, but WP could confuse other two parameter permalinks to be a category/page permastruct, resulting in no posts found errors even if the requested post exists. This is because the first term may not always be a category, for example a parent/child page permastruct and not category/page.
Taxonomy terms usually have a “base” parameter so the WP parser knows the next term is a category and not a page, tag, post, or whatever else non-category. It’s much better to have example.com/cat/category-term/page-slug/ than just example.com/category-term/page-slug/. The “cat” parameter leaves no ambiguity to what comes next. But if every possible permalink on your site will always include a category term as the first parameter, your desired permastruct will work. Use add_rewrite_rule() to set it up.
Ok, thanks.
I am now thinking along the lines of:
mysite.com/my-custom-post-type/my-page
Considering that my-custom-post-type will have children categories as well, is the above allowed in WP?
-
This reply was modified 6 years, 1 month ago by
mad_griffith.
Yes! It is fact the default way of handling CPT permalinks 🙂
Uhm, I don’t seem to get it!
How would you have some-cpt/my-page/ and some-cpt/my-category, provided some-cpt is the same cpt across both?
some-cpt/my-page/ is the default. some-cpt/my-category is not and would be difficult to implement unless ALL permalink second terms are always a category term, which of course conflicts with some-cpt/my-page/. You cannot have it both ways.