This is a repost of an earlier question I had, but I like to go into more details now to be able to fully solve this.
So again, with (normal) posts changing the permalink is as easy as going to Settings > Permalink and by changing it to anything you like such as the broadly used %category%/%postname%.html
. This all works. If only this was as easy for the custom post type and it’s taxonomies as well.
So this is what I like to accomplish.
- http://mywordpress.com/portfolio.html – Display all posts in all categories.
- http://mywordpress.com/portfolio/music/ – Display all posts in this category.
- http://mywordpress.com/portfolio/music/trance/ – Display all posts in this (child) category.
- http://mywordpress.com/portfolio/music/trance/Tiesto.html – Display the post.
What I got so far is the following:
- First I created a
custom_post_type
named project and ataxonomy
project_category. - I created a page Portfolio and gave it a Page Template that will list all of the
post_type = project
posts. How can I add .html to the permalink of pages? It now links to http://mywordpress.com/portfolio while I want http://mywordpress.com/portfolio.html. This should solve the first issue. - Then things will get tough, because we don’t know by forehand how many subcategories there, maybe there are even none. So the structure we are after is
portfolio/(CATEGORY PATH FROM TOP TO CHILD)/post.html
. I am still stuck at this point. I think I should somehow get the last category added to the path and pass that along the project_category, which should lead to a taxonomy.php file where I can process it properly. - I haven’t tackled this neither but it should have something to do with
%postname%.html
at the end of something.
I hope my problem is clear and that there are brave developers which will help me tackle this problem which has already keep me busy for four FULL days!
Ok I think I might have a solution. I have no idea if this is the right way to accomplish this, but as for now it’s the only thing that seems to work.
2 Different Methods:
*See notes in the bottom of this post.
for example, you want to have such permalink structure:
/MAIN_CATEGORY/SUB_CAT_2/Another_SUBCAT/my-post
At first, you may need to set permalinks to /%category%/%postname%. Then…
METHOD 1:
create STANDARD categories (
MAIN_CATEGORY
,SUB_CAT_1
,and etc..
) , and register the CUSTOM POST, including this parameter:and use this codes to change permalinks: https://wordpress.stackexchange.com/a/195643/33667
Then, after publishing a CUSTOM POST (if attached under a category), URL will be:
example.com/MAIN_CATEGORY/SUB_CAT_1/my-post
METHOD 2:
(p.s. this method is not advised if you plan to publish hundreds or thousands of posts)
register the CUSTOM POST (named
MAIN_CATEGORY
), including these parameters:then, publish custom posts like this:
(i.e. publish several custom posts, named
SUB_CAT_2, SUB_CAT_1..
. After then, when you publish another post, but chooseSUB_CAT_2
as parent.p.s.
1) If you are newbie, review: Register CUSTOM POST and Registering CUSTOM POST with TAXONOMY
2) if you will need search functionality for sub-levels, then use custom search query
After banging my head for hours, I found this plugin very helpful.
https://wordpress.org/plugins/custom-post-type-permalinks/
Below is the screen shot of options it gives.